Twitter for Android

After searching for so long I still couldn’t find any decent library for Android that is as easy to use as the one supplied by Facebook, so I decided to create my own and publish it as open source. It’s still not complete and more work is needed to make it fully featured, but it is a good start. You can find it on google code.


Twitter for Android is based on Twitter4J, OAuth libraries, Facebook design and some code from Do it Yourself Android blog. I’ve used the FbDialog class from Facebook Android library and modified it to work with Twitter.

The basic flow :

  1. Retrieve the request token using OAuthRpovider.
  2. Load the response URL inside a Web View.
  3. User authorizes your app.
  4. Retrieve access token and store it in shared preferences.


mUseTwitter = (CheckBoxPreference) findPreference("use_twitter");
// In case user clicks on twitter authenticate it.
mUseTwitter.setOnPreferenceClickListener(new OnPreferenceClickListener() {
	public boolean onPreferenceClick(Preference preference) {
		if (mUseTwitter.isChecked()) {

			// Make sure to pass the consumer key and secret you got back from Twitter.
			// Use dialog listener to act uppon errors.
			TwitterAuthDialog dialog = new TwitterAuthDialog(activity, Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, new TwitterDialogListener() {

				public void onComplete(Uri values) {}

				public void onTwitterkError(TwitterException e) {
					String error = e.getMessage();
					Log.e(Constants.TAG, error);
					// Uncheck the checkbox.


				public void onError(TwitterDialogError e) {
					String error = e.getMessage();
					Log.e(Constants.TAG, error);
					// Uncheck the checkbox.

				public void onCancel() {
					// Uncheck the checkbox.


			// Show the authentication dialog.;

		return true;

The code demonstrates how to authenticate the user when he/she presses a Twitter checkbox in a preferences window. The most important line is #9 where you actually create the dialog box and pass the key and secret which are mandatory to the OAuth process. Whenever the user finishes the authentication appropriate events will be triggered.

Outstanding issues

  • Unfortunately unlike Facebook, Twitter does not give any indication that the user clicked cancel during the authentication process, therefore the dialog box will not automatically dismiss itself which might cause confusion for the user.
  • Size too big.


  1. Twitter4J
  2. Facebook SDK
  3.  Do it Yourself Android blog
  4. Download now the source code

1 Comment

Filed under Twitter for Android

One response to “Twitter for Android

  1. Pingback: Twitter for Android – reusing Facebook to create a better experience | Android JB

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s