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