Android Show Tweets

Android Show Tweets

In previous three tutorials we have learnt so many things about Twitter APIs which includes:

1. Twitter Integration.

2. Show different Timelines.

3. Compose Tweets.

So Today we will learn a bit more about Twitter API i.e Show Tweets. TweetUi API provides Tweet views to render Tweets in your app via code or in XML.

Features

  • Two variants to choose from:
    TweetView: Standard Tweet view with photo forward.
    CompactTweetView: Tweet view with cropped photo, for use in list displays.
  • Tweet action buttons (favourite, share) can optionally be enabled.
  • Tap Tweet view to open Tweet permalink using default url intent.
  • Tweet views automatically use user authentication if available or guest authentication.

Before moving further please go through all my three previous articles about Twitter to get a better understanding.

VIDEO DEMO

Tweet Views

TweetUi provides the TweetView and CompactTweetView views for rendering Tweets. The view constructors take a context, the Tweet to be rendered, and an optional style.

Tweets can be requested through the TwitterCore Tweet API or TweetUtils which will cache recent requests. Here is an example using TweetUtils to load a Tweet and construct a TweetView and CompactTweetView in the success callback.

TweetView

CompactTweetView

TweetViews using Code

TweetViews using Code

XML

The TweetView and CompactTweetView can be inflated from an XML layout as well. For this usage, the tw__tweet_id attribute must specify the Tweet id and the Tweet will be loaded and rendered upon inflation.

TweetView

Tweet View XML

Tweet View XML

CompactTweetView

NOTE : You must use the layout parameters android:layout_width=”match_parent” and android:layout_height=”wrap_content”.

Compact Tweet View XML

Compact Tweet View XML

Tweet Actions

You can handle the Tweet Views actions by setting a callback method which you can listen and do the stuff according to that. On setting a Callback method you can listen events like favourite event. If user is guest user then you have to ask user to do login first. Twitter will throw an exception TwitterAuthException by which you get to know that user is not authenticated. Below is the code

After creating a callback set the callback to Tweet like below:

If you have implemented Tweet using XML then use below code to set callback :

Styling

You can style TweetViews by using simple line of code. Use below code to set style in java code.

Use below code to set style in XML code:

Example

1. Create an App on Twitter and get API Key and Secret. To know how to get these things check my previous tutorial.

2. After getting API Key and Secret open build.gradle and add the following dependencies to it and sync the gradle.

3. On syncing completion create an Application java class naming MyApplication.java where we will initialise Twitter.

4. Now declare create Application in AndroidManifest.xml.

5. Lets create an xml layout naming activity_main.xml and place the following code into it. In this class I have taken two Tweet Views one TweetView and another CompactTweetView.

If you don’t know how to get TweetId from a Tweet see the below image:

Click any one Tweet

Click any one Tweet

Copy Tweet ID from Link

Copy Tweet ID from Link

6. Now open MainActivity.java and add the below code to it.

7. Don’t forget to add INTERNET permissions to AndroidManifest.xml.

8. Finally all done , see so simple. Now you can also create TweetViews.

Thanks. :)

Download Source Code

NOTE : After downloading don’t forget to place your API Key and Secret inside MyApplication.java.

 

About the author

I am an Android developer for whom Android is the one and only love. I work as a freelancer which gets me and Android closer. I always love to share my learnings and love to learn from others knowledge too.

COMMENTS (2)
Reply

Is it okay to have CONSUMER_SECRET in app side code?

Leave a Comment

Comment (required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Name (required)
Email (required)