Twitter Integration in Android

In this tutorial, i’ll explains how to integrate twitter in android application. The example below using twitter4j java library for login to twitter and allows to post text and image in users twitters timeline.

Create New App in Twitter console

  1. Register for your Twitter App at here.
  2. On your dashboard, Create new App and enter details for your app and don’t forget to give some dummy url in the callback url field.
    twitter new app
  3. Under the permission tab change the access type to Read and Write.
    twitter permission
  4. Under the Keys & Access tab, Copy Consumer Key & Consumer Secret key which are required in your application.
    twitter keys


Download Twitter SDK

Twitter4J is an unofficial Java library for the Twitter API. With Twitter4J, you can easily integrate your Java application with the Twitter service.

You need to download sdk from here and extract and copy the core jar file to your libs folder. Now on Android Studio use compile files('libs/twitter4j-core-4.0.4.jar')



On Android Application, add the permissions in the AndroidManifest.xml.

Now for OAuth Connection with Twitter, add this IntentFilter to your login activity.


Login with Twitter Account:

To Login into Twitter Account, use RequestToken on AsyncTask.

On Successful login in into account, the onActivityResult will return a AccessToken which need to store for future login purpose. You can also save this in your MySQL Database if required.


Logout from Twitter Account

To Logout from Twitter Account, from just clear the access tokens from your application shared preferences. Please remember this logout code won’t logout user from the twitter in the browser.


Accessing User Details:

To access user details, get a user Id from the access token. Create a User interface from user id on Twitter interface with showuser() method. From User interface, we can access the user details.


Post a New Tweet on Twitter Status:

To post a new Tweet on Twitter, get the accesstoken which is required by the TwitterFactory to build a Twitter Interface. Finally use the updateStatus method from Twitter method to post a new Tweet.

twitter twitter_login twitter_home twitter_new_tweet

Complete Source code available here.

About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.