Android Show Tweets - Androhub

Show Tweets Banner

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. :)

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

 

5 Comments

Narendra
Sunday, April 15th, 2018

Is it okay to have CONSUMER_SECRET in app side code?

Dr. Droid
Sunday, April 15th, 2018

Hi Narendra,

No it is not okay to have your any API KEYs inside app code. There is a way to hide your API Keys , I am sharing one link just go through it.

https://medium.com/code-better/hiding-api-keys-from-your-android-repository-b23f5598b906

Thanks

Rozy Mumbai
Tuesday, November 6th, 2018

hi

Nani
Wednesday, December 2nd, 2020

Hi,
thanks for the tutorial,
In this tutorial showing like and share options only. i want to show retweet options to my users for retweet the post. Please reply
Thanks in advance

Dr. Droid
Wednesday, December 2nd, 2020

Hi Nani,

This is the UI provided by Twitter SDK so only the Like and share buttons are only available.

Thanks

Post comment

Your email address will not be published. Required fields are marked *