Android Twitter Integration - Androhub

Twitter Integration Banner

Android Twitter Integration

Hello to all, today we are going to learn how to implement Twitter Integration in our Android app and let user do Sign In in single click.

Twitter Integration lets users sign in to your Android app with their existing Twitter account and get their profile information like name, email, profile pic and other details.

This tutorial includes:

1. Login using TwitterLoginButton.

2. Login using Custom button.

3. Fetch user Email.

4. Fetch user Profile picture.

VIDEO DEMO

So lets start integrating Twitter in our Android app. So first of all we need to create an App on Twitter.

Create an App on Twitter

1. To create an App got to this link and follow the step by step process.

Please check my video first part to see how to create an App on Twitter.

2. After creating app you will get two things from it i.e API Key and API Secret. Copy both the API Key and Secret and place in Android project inside string.xml.

Now after creating an app and getting API Key and Secret move to Android Studio and follow the below steps:

1. First of all open the build.gradle file and add the below dependency to it to use Twitter APIs in our app.

Read more about Twitter APIs here.

2. Now create a xml layout naming activity_main.xml and add the below code to it. In this class i have taken 3 Button  : 1 for Default Twitter Login, 2 for Custom Login Button and 3 to Fetch user profile image as Twitter won’t give profile image in default login process.

3. Once you’ve included the Twitter Kit dependencies you must initialise Twitter Kit. For this create custom Application class you can initialize Twitter Kit in the onCreate() method.

After creating class you have to declare Application class in manifest file inside application tag like below:

4. Next, add your API key and secret which you got after creating app on Twitter to to your application strings.xml resources.

There are some extra strings that i am using in my app if you want to add them too you can:

5. To implement Twitter login you can use TwitterLoginButton by pasting the below xml code into your xml layout file.

6. After adding TwitterLoginButton to xml layout use the attach the below code to your Activity or Fragment.

7. Next, pass the result of the authentication Activity back to the button using below code.

NOTE : If your are using the TwitterLoginButton in a Fragment, use the following steps instead in your Activity.

8. By doing above steps you will be able to authenticate your app with Twitter account. Now if you want to access the User Email as well then use the below code after authenticating Twitter Account.

NOTE : Before using this feature, ensure that “Request email addresses from users” is checked for your Twitter app and User is having TwitterSession.

Authenticate Twitter
Authenticate Twitter

 

Authentication Started
Authentication Started

 

Authentication Successful
Authentication Successful

9. Once the user authentication successful, 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.

10. If you want to fetch User other details including Profile picture (as in all the above APIs Twitter doesn’t provide User profile picture), user name ,etc the use the below code once user is successfully authenticated else the API won’t work.

Fetched User Profile Image
Fetched User Profile Image

11. Finally full source code of MainActivity.java includes:

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

13. Finally all done , now you can also integrate Twitter to your Android app.

Thanks. :)

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

7 Comments

Soon
Friday, August 31st, 2018

I found almost no information about twitter login for android, and also I could not find the twitter’s documentation guide that teachs how to integrate twitter login in android. Then, you saved me. Thank you for the great article!

António Pereira
Thursday, May 23rd, 2019

Got a lot of errors like:
Duplicate class android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat found in modules classes.jar (com.android.support:support-compat:28.0.0) and classes.jar (com.android.support:support-v4:23.1.1)

Any idea?

Dr. Droid
Friday, May 24th, 2019

Hi Antonio,

You have to use only one dependencies with same like as you mentioned com.android.support:support-compat:28.0.0 com.android.support:support-v4:23.1.1 both are same and having different versions. So try to remove one and sync again.

Thanks

Vikram
Tuesday, June 2nd, 2020

E/Twitter: Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
at com.twitter.sdk.android.core.identity.AuthHandler.handleOnActivityResult(AuthHandler.java:98)
at com.twitter.sdk.android.core.identity.TwitterAuthClient.onActivityResult(TwitterAuthClient.java:143)
at NewMyProfileFragment.onActivityResult(NewMyProfileFragment.kt:431)

Dr. Droid
Friday, June 5th, 2020

Hi Vikram,

Make sure you followed each step and added a callback scheme as well?

Thanks

Yumna Desai
Tuesday, November 16th, 2021

Hi, do you know of a link to show me how to embed tweets from a certain account on Andriod studio?

Dr. Droid
Tuesday, November 23rd, 2021

Hi Yumna,

Please have a look here: https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/introduction

Thanks

Post comment

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