Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in
Twitter Logo

Android Twitter Integration

Twitter is an online news and Social Networking Service where users can post and read messages from other users. Twitter messages, tweets as they are called have a limit of 140 characters. Twitter has already become one of the most used messaging services because it caters to both official and unofficial communication.

In this blog, we will be discussing how can we integrate Android Apps with Twitter. We will be logging into our app using twitter and fetching the email id of the user in the twitter account.We will also be discussing how to make the app fetch tweets of a particular twitter account. Steps to develop this can be summarized below -

Used Concepts & Used Version: (Version of Android API, Twitter API)

We will be using Twitter Developer Kit here which is provided by Twitter Devlopers website.

Now, let us begin the development...

1.Register your app on Twitter website

Picture 1 Picture 2 Picture 3 Picture 4 Picture 5

2. Include the dependencies required for integration

Run ‘Sync Gradle’ after you add the dependencies.

3.Add the Twitter Login Button

Twitter login Button is available from the Twitter Dependency that we added earlier. We add another button to show the tweets from a particular account which we will code later.

4.Initialize Twitter inside OnCreate Method and Handle the callback when the user clicks on login button

a) We initialize Twitter inside onCreate method before calling setContentView(R.layout.activity_main). If initialization is done after calling setContentView - you will find that the twitter login button is devoid of its blue color. While configuring Twitter for this app , we also provide the keys and access token which we got from the twitter website earlier.

b) We set a callback on the login button to handle a click by the user on the ‘Login by Twitter Button’. Inside the callback we create a sessions and can use the token/secret code if required.In this case we are showing it in the Toast Message.

c) Use a TwitterAuthClient to fetch email of the user. Using TwitterAuthClient we are trying to fetch email id of the user . An email id will be aprovided only if the user registered with an email id in their twitter account. Also, you need to have given a permission in the apps.twitter link which we dealt with earlier. Here we display the email id using a Toast message.

d) Use Twitter’s Composer Activity to create a tweet dialog with a Tweet button.

ShowTweetActivity.class

We have another button in the MainActivity’s layout which can be clicked to see a particular account’s tweets in a list. We do this in the ShowTweetActivity.class

a) This activity extends a ListActivity and uses a TwitterListAdapter to set the data on the list. Below is the layout that we use for this activity.

b) Make sure the id that you provide to the list view is not the general id using @+id. You need to use the id from the Android SDK.

Layout for ShowTweetActivity.class

Output when the App is run -

  1. User is presented with the Login Screen where the user can click on the Twitter Login button to login.
  2. User is taken to another screen where they can authorize your app to use credentials of Twitter to allow login.
  3. After the user authorizes, the user is now being directed back to the app
  4. When the user reaches back to the app, they are presented with twitter composer with a composed tweet. Also , during this process the email id of the user is displayed on the screen.
  5. The user can now click on Tweet Button and post this tweet on their twitter timeline.

  6. On the mainscreen , if the user clicks on the show tweet button , they can see the twitter timeline of the particular account which has been hardcoded in the program.