Twitter Login in Android

Twitter Login in Android

Integrating Twitter in your Android application will make user easily login into your app using their Twitter account which avoids filling a long registration forms.

Traditionally, authentication has required the user to complete a registration form, usually creating a username and password in the process. However, with the rise of social networks there’s now a much quicker and easier way of getting users signed into your app: using an account they’ve already created with an external authentication provider, such as Facebook or Twitter.

Twitter Login in Android

In this tutorial, I’m going to show you how to replace the time-consuming and frustrating user registration form, with a simple ‘Sign in with Twitter’ button.

Users who have just downloaded your Android app are going to be a lot happier if you allow them to log in using a popular social networking service instead of asking them to fill out a registration form. In this quick tutorial, you are going to learn how to enable your users to sign into your app with their Twitter accounts.

To follow along, you should have the following set up:

Any app that needs to interact with Twitter’s APIs should be registered using the Twitter Application Management console. Use your Twitter account to log in.

Click on the Create New App button, and fill out the form. Enter meaningful values in the Name and Description fields, because these will be shown to your app’s users. We won’t be needing the Website and Callback URL for this tutorial, but you can’t leave them blank. If you have your own website, you can use its address here. If you don’t,  you can simply type in

Accept the agreement and submit the form. Your app is now registered.

Click on the Keys and Access Tokens tab and make note of your Consumer Key and Consumer Secret. We’ll be needing them later.

Getting Started

The Android SDK is a collection of individual libraries designed to make interacting with Twitter seamless and efficient. This includes:

  • TwitterCore for Twitter login and API access.
  • TweetUi for displaying Tweets and Timelines.
  • TweetComposer for creating Tweets.
  • Twitter + MoPub for monetizing Twitter timelines with MoPub.

Twitter requires that all API requests be authenticated with tokens that can be traced back to an individual Twitter App.

Add Twitter Kit Dependencies

To get started you will need to add the following dependencies to your application’s Gradle config (usually app/build.gradle).

All the Twitter Kit artifacts are distributed through Bintray jcenter. Make sure jcenter is included in your repositories scope. This is default in all new versions of Android Studio.

Initialize Twitter Kit

Once you’ve included the Twitter Kit dependencies you must initialize Twitter Kit. If using a custom Application class you can initialize Twitter Kit in the onCreate() method and initialize with custom configuration. (Create a new Java Class and name it App).

Replace the Consumer Key and Consumer Secret with the ones generated earlier on. Then add it to your Manifest file as shown below.

We initialize it using the “name” namespace, inside the Application Tag as shown above. Remember to also add the INTERNET Permission before you proceed.

Once Twitter kit has been initialized TwitterCore, TweetUi, and TweetComposer can be accessed through their singleton accessor.

  • TwitterCore.getInstance()
  • TweetUi.getInstance()
  • TweetComposer.getInstance()

Log In with Twitter

TwitterCore provides Log In with Twitter. This feature enables application users to authenticate with Twitter.

Before using this feature, ensure that “Sign in with Twitter” is enabled for your Twitter app (

When attempting to obtain an authentication token, TwitterCore will use the locally installed Twitter app to offer a single sign-on experience. If TwitterCore is unable to access the authentication token through the Twitter app, it falls back to using a web view to finish the OAuth process.

The simplest way to authenticate a user is using TwitterLoginButton. Inside your layout, add a Login button with the following code:

The User Interface will look like this: The red widget represents an ImageView, to display the logged in user Profile Image.


In the Activity or Fragment that displays the button, you need to create and attach a Callback to the Login Button.

Next, pass the result of the authentication Activity back to the button:

Using Twitter Button Inside a Fragment

Inside the Activity hosting the Fragment, pass the result from the Activity to the Fragment.


If login completes successfully, a TwitterSession is provided in the success result. This TwitterSession will contain a token, secret, username, and user ID of the user and becomes the active session and is automatically persisted. If you need to retrieve the TwitterSession at a later time, you may do so using the SessionManager.

Request User Email Address

To request a user’s email, call the TwitterAuthClient#requestEmail method, passing in a valid TwitterSessionand Callback.

If the email address is available, the success method is called with the email address in the result. It is not guaranteed you will get an email address.

Before using this feature, ensure that “Request email addresses from users” is checked for your Twitter app.

For example, if someone signed up for Twitter with a phone number instead of an email address, the email field may be empty. When this happens, the failure method will be called because there is no email address available.

Show Twitter Names and Profile Picture

In this example, I will show you how to show the User’s Twitter Name and display their Profile Image in our ImageView, as shown below.


We then click the Login with Twitter Button, we are prompted to Authorize the app to have access to your Twitter account as shown below:


After successful login, we can set the Twitter Username to our TextView as shown below:

The complete code, for the MainActivity looks like this:


You need to add Glide for this code to work, but you can also choose to work with Picasso or whichever Image Library suits you. I would however recommend using Glide. We are using our Twitter Session to get the User object, i.e The Logged In User data and then use it accordingly. In the above example, I am only using the name and the profile picture of the user. You can look at the body object to see what is packed as our response.

This article has been updated, thanks to a request by Raja 🙂 Thanks Buddy! Appreciated


Your app is now complete. When you build it and deploy it on your Android device, you will see the Twitter login button. Tapping the login button takes you to a Twitter page that asks you to log in and authorize the app. After successfully logging in, the user’s name and profile image will be displayed.

In this article, you learned how to use the Twitter SDK to add Twitter Login to your Android app. In the next article, we will look at Google Sign In option, available for Android. Remember to subscribe to get updates instantly and also leave questions or comments below, and I will be glad to help you.

Happy Coding!

9 Replies to “Twitter Login in Android”

    1. Hi Raja
      Thanks for the review and comments. I have updated the article to suit your requirements, kindly check the article again and leave further questions if any.
      Happy Coding!

Leave a Reply

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