Twitter Integration in Android App Using Fabric

Introduction

 
Integrating Twitter into an Android application will make it easy for a user to login with Twitter using an email id and password and get user information like name, user profile URL and location. Use the following procedure to make an application like that.
 
Step 1: Register app on Twitter 
  1. Please go to https://dev.twitter.com/apps/new and create a new application.
     
     
  2. Go to the permission tab and update the setting for reading, Write and Access of direct messages.   

      
  3. Copy the consumer key and secret key.
     
Step 2: Configure Fabric in eclipse
  1. Go to Help -> Install New Software.
     
  2. Paste in the URL https://fabric.io/download/eclipse.
     
     
  3. Uncheck the "Contact all update sites" checkbox and press the Next button to install.
Step 3: Sign Up With Fabric
  1. If you do not have an account then sign up with fabric.
  2. Go to https://get.fabric.io/.
     
      
  3. Register on fabric.
Step 4: Create a New Project
  1. Create a new project in Eclipse using File New -> Android -> Application Project and enter the project name.
Step 5: Configure fabric on a project
 
After installing fabric in Eclipse.
 
1. Login with fabric.

 
2. Select your project.

3. Press the Next button and install it.
     
4. Open activity_main.xml and paste in the following code.
         
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  2.     android:layout_width="fill_parent"    
  3.     android:layout_height="fill_parent" >    
  4.     
  5.     <ImageView    
  6.         android:id="@+id/imageView_profile_image"    
  7.         android:layout_width="80dp"    
  8.         android:layout_height="80dp"    
  9.         android:layout_alignParentTop="true"    
  10.         android:layout_centerHorizontal="true"    
  11.         android:layout_marginTop="56dp"    
  12.         android:scaleType="fitXY"    
  13.         android:src="@null" />    
  14.     
  15.     <TextView    
  16.         android:id="@+id/textView_name"    
  17.         android:layout_width="wrap_content"    
  18.         android:layout_height="wrap_content"    
  19.         android:layout_below="@+id/imageView_profile_image"    
  20.         android:layout_centerHorizontal="true"    
  21.         android:layout_marginTop="16dp" />    
  22.     
  23.     <TextView    
  24.         android:id="@+id/textView_location"    
  25.         android:layout_width="wrap_content"    
  26.         android:layout_height="wrap_content"    
  27.         android:layout_below="@+id/textView_name"    
  28.         android:layout_centerHorizontal="true"    
  29.         android:layout_marginTop="16dp" />    
  30.     
  31.     <com.twitter.sdk.android.core.identity.TwitterLoginButton    
  32.         android:id="@+id/login_button_twiter"    
  33.         android:layout_width="wrap_content"    
  34.         android:layout_height="wrap_content"    
  35.         android:layout_alignBottom="@+id/textView_location"    
  36.         android:layout_centerHorizontal="true"    
  37.         android:layout_marginTop="16dp" />    
  38.     
  39. </RelativeLayout>   
    5. Open MainActivity.java and paste in the following code. 
    1. package com.mcnsolutions.twitterwithfabric;    
    2.     
    3. import java.io.InputStream;    
    4. import retrofit.http.GET;    
    5. import retrofit.http.Query;    
    6. import android.app.Activity;    
    7. import android.content.Intent;    
    8. import android.graphics.Bitmap;    
    9. import android.graphics.BitmapFactory;    
    10. import android.os.AsyncTask;    
    11. import android.os.Bundle;    
    12. import android.util.Log;    
    13. import android.view.View;    
    14. import android.widget.ImageView;    
    15. import android.widget.ProgressBar;    
    16. import android.widget.TextView;    
    17.     
    18. import com.twitter.sdk.android.Twitter;    
    19. import com.twitter.sdk.android.core.Callback;    
    20. import com.twitter.sdk.android.core.Result;    
    21. import com.twitter.sdk.android.core.TwitterApiClient;    
    22. import com.twitter.sdk.android.core.TwitterAuthConfig;    
    23. import com.twitter.sdk.android.core.TwitterAuthToken;    
    24. import com.twitter.sdk.android.core.TwitterException;    
    25. import com.twitter.sdk.android.core.TwitterSession;    
    26. import com.twitter.sdk.android.core.identity.TwitterLoginButton;    
    27. import com.twitter.sdk.android.core.models.User;    
    28.     
    29. import io.fabric.sdk.android.Fabric;    
    30.     
    31. public class MainActivity extends Activity {    
    32.     private TwitterLoginButton loginButtonTwitter;    
    33.     private TextView name, location;    
    34.     private ImageView profileImageView;    
    35.     private ProgressBar pb;    
    36.     private static final String TWITTER_KEY = "Paste Consumer Key";    
    37.     private static final String TWITTER_SECRET = "Paste Secret Key";    
    38.     
    39.     @Override    
    40.     protected void onCreate(Bundle savedInstanceState) {    
    41.         super.onCreate(savedInstanceState);    
    42.         TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY,    
    43.                 TWITTER_SECRET);    
    44.         Fabric.with(thisnew Twitter(authConfig));    
    45.         setContentView(R.layout.activity_main);    
    46.         name = (TextView) findViewById(R.id.textView_name);    
    47.         location = (TextView) findViewById(R.id.textView_location);    
    48.         // pb = (ProgressBar) findViewById(R.id.progressBar1);    
    49.         profileImageView = (ImageView) findViewById(R.id.imageView_profile_image);    
    50.         loginButtonTwitter = (TwitterLoginButton) findViewById(R.id.login_button_twiter);    
    51.     
    52.         loginButtonTwitter.setCallback(new Callback<TwitterSession>() {    
    53.             @Override    
    54.             public void success(Result<TwitterSession> result) {    
    55.     
    56.                 TwitterSession session = Twitter.getSessionManager()    
    57.                         .getActiveSession();    
    58.                 loginButtonTwitter.setVisibility(View.GONE);    
    59.                 getTwitterData(session);    
    60.     
    61.             }    
    62.     
    63.             @Override    
    64.             public void failure(TwitterException exception) {    
    65.                 // Do something on failure    
    66.             }    
    67.         });    
    68.     
    69.     }    
    70.     
    71.     public void getTwitterData(final TwitterSession session) {    
    72.         MyTwitterApiClient tapiclient = new MyTwitterApiClient(session);    
    73.         tapiclient.getCustomService().show(session.getUserId(),    
    74.                 new Callback<User>() {    
    75.                     @Override    
    76.                     public void success(Result<User> result) {    
    77.                         // Do something with result, which provides a Tweet    
    78.                         // inside of result.data    
    79.     
    80.                         TwitterAuthToken authToken = session.getAuthToken();    
    81.                         String token = authToken.token;    
    82.                         String secret = authToken.secret;    
    83.                         name.setText(result.data.name);    
    84.                         location.setText(result.data.location);    
    85.                         new ImageDownloader(profileImageView)    
    86.                                 .execute(result.data.profileImageUrl);    
    87.     
    88.                     }    
    89.     
    90.                     public void failure(TwitterException exception) {    
    91.                         // Do something on failure    
    92.                     }    
    93.                 });    
    94.     
    95.     }    
    96.     
    97.     class MyTwitterApiClient extends TwitterApiClient {    
    98.         public MyTwitterApiClient(TwitterSession session) {    
    99.             super(session);    
    100.         }    
    101.     
    102.         public CustomService getCustomService() {    
    103.             return getService(CustomService.class);    
    104.         }    
    105.     }    
    106.     
    107.     interface CustomService {    
    108.         @GET("/1.1/users/show.json")    
    109.         void show(@Query("user_id"long id, Callback<User> cb);    
    110.     }    
    111.     
    112.     @Override    
    113.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
    114.         super.onActivityResult(requestCode, resultCode, data);    
    115.     
    116.         // Pass the activity result to the login button.    
    117.         loginButtonTwitter.onActivityResult(requestCode, resultCode, data);    
    118.     }    
    119.     
    120.     class ImageDownloader extends AsyncTask<String, Void, Bitmap> {    
    121.         ImageView bmImage;    
    122.     
    123.         public ImageDownloader(ImageView bmImage) {    
    124.             this.bmImage = bmImage;    
    125.         }    
    126.     
    127.         protected Bitmap doInBackground(String... urls) {    
    128.             // pb.setVisibility(View.VISIBLE);    
    129.             String url = urls[0];    
    130.             Bitmap mIcon = null;    
    131.             try {    
    132.                 InputStream in = new java.net.URL(url).openStream();    
    133.                 mIcon = BitmapFactory.decodeStream(in);    
    134.             } catch (Exception e) {    
    135.                 Log.e("Error", e.getMessage());    
    136.             }    
    137.             return mIcon;    
    138.         }    
    139.     
    140.         protected void onPostExecute(Bitmap result) {    
    141.             // pb.setVisibility(View.GONE);    
    142.             bmImage.setImageBitmap(result);    
    143.         }    
    144.     }    
    145. }   
      6. Run the application.