Android Twitter Show Timelines - Androhub

Twitter Show Timelines Banner

Android Twitter Show Timelines

In previous article we have leant integration of Twitter with our Android App. Today we are going to move further in Twitter Integration by showing different kind of Timelines. Twitter API provides Timelines for adding scrollable timelines anywhere in your app.

NOTE : Please go through the previous tutorial about Android Twitter Integration to check how to get Twitter API Key and Secret Key and how to implement login in Android.

Prerequisite

1. View Pager with Tab Layout

2. Fragments

3. Shared Preferences

4. RecyclerView

5. SwipeRefreshLayout

In this article we are going to cover following things:

VIDEO DEMO

Twitter provides following Timelines:

1. UserTimeline which shows the @sonusurender0 (my twitter account) user’s timeline of Tweets or any logged in user timeline Tweets.

2. SearchTimeline which shows Tweets matching the query “#android” or any searched query.

3. CollectionTimeline for the National Parks Tweets example collection of Tweets.

4. TwitterListTimeline for the @twitterdev national-parks list can be created with the owner screen name (or user id) and list name.

5. FixedTweetTimeline which shows a fixed set of Tweets.

Creating Timeline

With the help of above code snippets you can easily create any kind of Timeline according to your requirement.

Now after creating Timeline next step is to show the created TimeLine into your RecyclerView or ListView.

1. With RecyclerView : Make sure you use LinearLayoutManager with VERTICAL orientation.

2. With ListView

Tweet Action Callback

You can listen Tweet action callback too by adding setOnActionCallback with Adapter. To do this you should have taken Access of “Read, Write and Access direct messages” for Twitter App shown below:

Permission Screen
Permission Screen

Filter Timeline

Twitter provide functionality to filter Tweets displayed in our app. You can use this functionality to prevent showing Tweets with profane words, hide Tweets that link to blacklisted URLs, or block particular user’s Tweets from appearing in your app. The rules can be managed in a standard JSON configuration file or creating List for different filters.

Following are the different Filter Values:

  • keywords : Removes Tweets containing specified keywords in Tweet text. Uses localized case-insensitive matching.
  • hashtags : Removes Tweets containing specified hashtags. Uses localized case-insensitive matching.
  • handles : Removes Tweets from specified user or replies to specified user. Uses case-insensitive matching.
  • urls : Removes Tweet containing URLs from specified domain. Supports internationalised domain names.

Method 1 : By creating List of different Filters.

You can pass null in FilterValues. Suppose you want to remove blacklist URLs tweets then pass other Filters as null like below:

Method 2 : Using JSON

Create a .json file under res=>raw directory and add your Filter values.

After creating JSON file use the below code to read data from it and pass to Filter Values.

You can download json file from server and use it also depend upon your requirement.

Now after creating FilterValue create TimelineFilter using BasicTimelineFilter provided by Twitter.

After creating TimelineFilter too set it to your adapter to see effect.

Swipe Down to Refresh Tweets

Both TweetTimelineRecyclerViewAdapter and TweetTimelineListAdapter support swipe down to refresh tweets. For that you have to put your xml layout into SwipeRefreshLayout then use the below code to attach it with adapter.

If you don’t know about SwipeRefreshLayout then check this link.

Timeline Style

Twitter API supports 4 different kind of styles that you can use while creating adapter :

  • tw__TweetLightStyle
  • tw__TweetLightWithActionsStyle
  • tw__TweetDarkStyle
  • tw__TweetDarkWithActionsStyle

Custom Styling

Following are the attributes of the twitter styles

  • tw__container_bg_color :  background color of the Tweet
  • tw__primary_text_color : Tweet text color and author name color
  • tw__action_color : color of links in Tweet text
  • tw__tweet_actions_enabled : show or hide Tweet actions

To create custom style use the below code snippet and use the style name while creating adapter.

If any one of the custom style attributes is not set, the value defaults to the tw__TweetLightStyle‘s value.

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. Add the below given strings into your strings.xml which we will using in our app:

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

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

5. Now declare create Application in AndroidManifest.xml.

6. Lets create an xml layout naming login_activity.xml and place TwitterLoginButton inside it.

7. Now create LoginActivity.java and add the Twitter Login logic here. In this class we are checking if user is already logged-in then we are redirecting app to MainActivity else we are asking user to do login. To check user validation we are using SharedPreferences and saving user information to use later.

LoginActivity will be our launching activity so don’t forget to add it into AndroidManifest.xml file and make it a launcher activity.

NOTE : If you don’t want the user to Authenticate via Twitter then directly open MainActivity and Twitter will automatically treat it as Guest User.

Login Screen
Login Screen

8. For SharedPreferences create a class  naming MyPreferenceManager,java which will handle all SharedPreference methods.

9. For MainActivity.java contains setup of ViewPager and TabLayout which you can refer to this article.

10. Now creating Fragment Tabs to show different Timeline.

  • UserTimeline : To show logged-in user timeline tweets or for guest user show any Screen Name tweets.

user_timeline_fragment.xml

 UserTimelineFragment.java

User Timeline Tab
User Timeline Tab

  • SearchTimeline : To show timeline on basis of user search query.

search_timeline_fragment.xml

SearchTimelineFragment.java

Search Timeline Tab
Search Timeline Tab

  • CollectionTimeline : To show timeline of user created Collection in Twitter.

collection_timeline_fragment.xml

CollectionTimelineFragment.java

Collection Timeline Tab
Collection Timeline Tab

11. For EditText in search_timeline_fragment.xml use the below drawable naming edit_text_bg.xml:

12. Don’t forget to add INTERNET permission in AndroidManifest.xml.

13. Finally all done , now you can create an app which will show Tweets Timelines

Thanks. :)

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

Post comment

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