Objective
In this tutorial we will learn the Twitter integration with android application.
Step 1.
Introduction
This tutorial is all about integration of twitter in android application using Fabric sdk in android studio. In this tutorial I will describe, how to add Fabric sdk in android studio, integration of twitter, login and fetching details of logged In user from twitter.
Step 2.
Creating an application on Twitter.
Step 2.1.
Visit apps.twitter.com and click on Create New App.
Step 2.2.
Enter details and submit
Now, name your application and describe it in Description field. Give website URL and enter some dummy URL in Callback URL field. And Click Create Your Application.
Step 2.3.
Set access permissions
Now open Permission Tab and change access type to Read, Write and Access direct messages.
You have created an app on twitter. Now its time to create an Android project.
Step 3.
Create an Android project.
In your android Studio, goto File New ⇒ New Projects . Set your project name and the Package Name and click Next.
And now, select API 15: Android 4.0.3 or higher and create your new project.
Step 4.
Integrate Fabric SDK to Android Studio.
Now its time to integrate Fabric SDK to your android studio. You may also integrate it before creating your android project.
Step 4.1.
Open settings in Android Studio to browse repositories
Fabric offer IDE plugin to integrate SDK into android application. To download Fabric Windows and Linux users: Select “Settings” from the File Menu. And select plugin and click “Browse Repositories”.
Step 4.2.
Install the plugin.
Search “fabric for android” and install plugin.
When installation is complete, restart Android Studio to apply changes.
Step 5.
Open Fabric and install Twitter kit, to integrate login with twitter.
Step 5.1.
Open Fabric
To open Fabric, Click on Fabric given on right side of the Android Studio.
Step 5.2.
Install Twitter Kit.
Choose Twitter kit, to integrate login with twitter and install it.
When you try to install twitter kit it will ask to login, then click “I already have an account” and enter your twitter key and twitter secret key.
Now build.gradle should have the following code, to include the Maven repository and apply the Fabric plugin.
Add the TwitterCore kit to build.gradle dependencies.
The TwitterCore Kit provides Login with Twitter and the Twitter API.
Also include following compile dependencies in build.gradle, further we will require these dependencies to show the profile picture.
Now update AndroidManifest.xml file,add the following code to application tag
Here android:value is the API key that you will find when you visit www.fabric.io/settings/organizations
And now add following code to give internet permission
Final code in AndroidManifest.xml file should be
Step 6.
Login and Retrieving user information.
Step 6.1.
Adding Login button
Now add Login button to authenticate the user.
Create content_main.xml file in layout folder of your project and include this file in activity_main.xml by adding the below given code.
Now replace your content_main.xml file with the following code given below.
Step 6.2.
Twitter Keys for authentication
You have to create two string variables named TWITTER_KEY and TWITTER_SECRET, that will contain consumer key and consumer secret key(generated while creating app on Twitter) respectively.
and be sure the TwitterCore kit is included in your application’s Fabric.with() call.
Step 6.3.
Registering Twitter session callback.
To register a Twitter Session callback, Add callback handler in the Activity that display the button.
In my project I have added a callback handler in MainActivity class in onCreate method.
And make sure to pass onActivityResult() call back to loginButton. Add this code to MainActivity class.
Step 6.4.
Retrieving user information
To retrieve user information add the following code to the success method of Twitter Session callback.
Now you can retrieve user information using object of User class. I have retrieved user’s Screen Name, Username, timezone, location, description etc.
Now update content_main.xml file, finally it should include the following code
And few updation in MainActivity.java file also required
Final Code in MainActivity.java file should be
Step 6.5.
Resultant screens