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. In this tutorial i explained how to integrate twitter in your android application using twitter oAuth procedure. This is very basic tutorial with simple login and updating twitter status message.
Registering twitter app – getting Consumer Key & Consumer Secret
In order to implement twitter oAuth in your application you need twitter consumer key and consumer secret which are used to make twitter API calls. So register a new twitter application and get the keys. Check the following video about creating a twitter app
1. Go to https://dev.twitter.com/apps/new and register new application. Fill application name, description and website.
2. Give some dummy url in the callback url field to make the app as browser app. (If you leave it as blank it will act as Desktop app which won’t work in mobile device)
3. Under the settings tab upload icon and change the access type to Read and Write.
4. Copy Consumer Key & Consumer Secret key
Downloading twitter4j library
In this tutorial to integrate twitter i used one of the most popular libraries twitter4j. You can read their official documentation to know more about it.
1. Download & extract twitter4j library from twitter4j-android-2.2.6.zip (slimmed version for Android platform). Here is the direct link
Creating new Project
In this tutorial i explained basic twitter API calls login and updating status only. If you want to integrate more api calls like reading users tweet timeline, sending direct message etc., you need to include other required .jar files too. Read more Code Examples to get know the usage.
1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
After creating project open AndroidManifest.xml file and paste the following code. I added following code to manifest file
- INTERNET Permission
- ACCESS_NETWORK_STATE Permission
- and added an Intent Filter
2. Copy the required twitter4j .jar files into your project’s libs folder. In this tutorial i copied twitter4j-core-android-2.2.6 as it is the only required file in this tutorial.
3. Before getting into twitter integration i am including two quick class files to the project. Create a new class file called AlertDialogManager.java and paste the following code. This class file used to show alert dialog.
4. Create another class file named ConnectionDetector.java and paste the following code. This class is used to detect internet connection status.
5. Open your activity_main.xml file and type the following code. In the following code i am placing login button, update status edittext and update status button. By default all the ui elements will be hidden except login button.
6. Now open your main activity class (In my case MainActivity.java) file and type the following code. In the following code i am declaring required variables.
Login with Twitter Account
Login with twitter account in your android application involves following process.
- First user asked to authenticate into your application using their twitter account.
- Once user successfully logged into his twitter account we will receive his oauth token and oauth secret.
- oauth token and oauth secret will be stored in android application shared preferences.
- Whenever you need oauth tokens, you can read from shared preferences.
- Once user clicks logout button we will delete oauth token & secret from shared prefrences
7. Open your main activity and write click event for login button.
> In click event i am calling loginToTwitter().
> Once the user logins into twitter he will be redirected to your android app again with oauth verifier.
> Using oauth verifier we do another request to get access token and access token secret.
and code for loginToTwitter() function is
Update Twitter Status
8. Once you login with twitter account you can see interface to update the status. Open your main activity class write a click event for update status button.
> In the following code async thread updateTwitterStatus() is called on clicking update status button.
and code for updateTwitterStatus() async task is
Logout from Twitter
9. Open your main activity and write a click event for logout button. Please remember this logout code won’t logout user from the twitter in the browser. It will just clear the access tokens from your application shared preferences.
and code for logoutFromTwitter() function is
Final Code:
Final code of MainActivity.java
This image is for thumbnail purpose