Now a days social networking websites like Twitter& are becoming very popular,so integrating Twitter with app has become a necessity to make your application popular. We are going to do the same through this tutorial. The Twitter Connect SDK provides code which third-party developers can embed into their applications to connect to their Twitter accounts and exchange information with Android apps. It’s a way of embedding “social context” to an Android app, according to Twitter.

Create a Viewbased Application with name ‘Twitter Android Prj’.

Follow the following steps:

  • Download jtwitter.jar for Android
    http://www.winterwell.com/software/jtwitter.php
    • First you have to set up new Android project .
    • We have to add all JARs and Library files.
    • Import all files in the destination group folder.
    • To test import all JAR and library in case any miss.And compile.
  • Create your twitter Acoount.
  • Now Save project (Command +S). Build and Run Project.
  • enter user name and password & Click on Log In Button.
  • Initailly you will see the friends List.
  • Click on Twit button and you can post new tweets in input dialog click ok to post .
  • Click on Followers button and you eill see the list of followers.
  • Click on Favorites button and you eill see the list of favorite tweets.

Description :

  • Download jtwitter.jar
    • We have to add all JARs and Library files as below
      adding external jar library step 1 in Android project

      add_jar1

      adding external jar library step 2 in Android project

      add_jar2

    • Import all files destination group folder. It should also look as shown below
      imported Twitter library setup in Android project

      ImportedLib

      [java]
      Append Following code in .xml file for UI Design
      
      [xml highlight="42,62"][/xml][/xml]
      <;?xml version="1.0" encoding="utf-8"?>
      
      <ImageView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:src="@drawable/logo1"
      android:layout_gravity="center_horizontal"
      android:layout_marginLeft="0dip"
      android:layout_marginTop="20dip"
      />
      
      <!-- Code For Login Name And Password  -->
      <FrameLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#fff"
      android:layout_marginTop="70dip">
      <TableLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical"
      android:gravity="center_horizontal"  >
      <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="User Name"
      android:layout_marginLeft="40dip"
      android:textColor="#000"
      />
      <EditText
      android:layout_width="150dip"
      android:layout_height="wrap_content"
      android:layout_marginLeft="40dip"
      android:singleLine="true"
      android:id="@+id/main_username_edit_text"
      />
      </TableRow>
      
      <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Password"
      android:layout_marginLeft="40dip"
      android:textColor="#000"
      />
      <EditText
      android:layout_width="150dip"
      android:layout_height="wrap_content"
      android:layout_marginLeft="40dip"
      android:password="true"
      android:singleLine="true"
      android:id="@+id/main_password_edit_text"
      />
      </TableRow>
      
      </TableLayout>
      </FrameLayout>
      <Button
      android:layout_width="70dip"
      android:layout_height="wrap_content"
      android:text="Log In"
      android:id="@+id/main_loin_button"
      android:layout_marginTop="20dip"
      android:layout_marginLeft="130dip"
      />
      <!-- Code For Login Name And Password Ends -->
      </LinearLayout>
      [/java]

      Code Description :
      Login:

      [java language=""]
      my_twiter = new Twitter(userName,password);
      if(my_twiter == null){
      Toast.makeText(main.this, "Incorrect Login",
      Toast.LENGTH_LONG).show();
      }
      else {
      try {
      Log.v("Twitter Status :",my_twiter.getStatus()+"");
      Intent intent = new Intent(main.this,Twitter_Info_Activity.class);
      startActivity(intent);
      }catch(Exception e){
      e.printStackTrace();
      }
      }
      [/java]

      Tweet Button Click :

      [java language=""]
      setTitle("Tweet");
      AlertDialog.Builder inputDialog;
      inputDialog = new AlertDialog.Builder(Twitter_Info_Activity.this);
      inputDialog.setTitle("Enter Tweet");
      et_input = new EditText(Twitter_Info_Activity.this);
      et_input.setWidth(250);
      et_input.setHeight(30);
      inputDialog.setView(et_input);
      inputDialog.setPositiveButton("Ok",
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      String txt_tweet= et_input.getText().toString();
      main.my_twiter.setStatus(txt_tweet);
      Toast.makeText(Twitter_Info_Activity.this,
      "Tweet Successful",
      Toast.LENGTH_LONG).show();
      }
      });
      inputDialog.setNegativeButton("Cancle",
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      // TODO Auto-generated method stub
      }
      });
      inputDialog.show();
      }
      [/java]

      Friends Button Click :

      [java language=""]
      setTitle("Friends");
      List<User> arr= main.my_twiter.getFriends();
      Object[] str=arr.toArray();
      String[] str1 = new String[str.length];
      for(int i=0;i<str.length;i++) {
      str1[i] = str[i].toString();
      }
      lst_myTwitts.setAdapter(new ArrayAdapter<String>(Twitter_Info_Activity.this,
      android.R.layout.simple_list_item_1, str1));
      [/java]

      Followers Button Click :

      [java language=""]
      setTitle("Followers");
      List<User> arr= main.my_twiter.getFollowers();
      Object[] str=arr.toArray();
      String[] str1 = new String[str.length];
      if(str1.length == 0) {
      Toast.makeText(Twitter_Info_Activity.this, "No Followers",
      Toast.LENGTH_LONG).show();
      }
      for(int i=0;i<str.length;i++) {
      str1[i] = str[i].toString();
      }
      lst_myTwitts.setAdapter(new ArrayAdapter<String>
      (Twitter_Info_Activity.this,
      android.R.layout.simple_list_item_1, str1));
      [/java]

      Favorite Button Click :

      [java language=""]
      setTitle("Favorites");
      List<Status> arr= main.my_twiter.getFavorites();
      Object[] str=arr.toArray();
      String[] str1 = new String[str.length];
      if(str1.length == 0) {
      Toast.makeText(Twitter_Info_Activity.this,
      "No Favorites", Toast.LENGTH_LONG).show();
      }
      for(int i=0;i<str.length;i++) {
      str1[i] = str[i].toString();
      }
      lst_myTwitts.setAdapter(new ArrayAdapter<String>(Twitter_Info_Activity.this,
      android.R.layout.simple_list_item_1, str1));
      [/java]

       

      [java][/java]
    • Now Save project (Command +S). Build and Run Project.

    Simulator will look like as follows

    • Login Screen : enter your twitter user name and password
      Twitter login screen in Android app

      LoginScreen

    • Friends Screen : shows your friends list.
      Twitter friends list display in Android app

      FriendsList

    • Tweet : Click on Tweet Button
      tweet selection interaction in Android app

      TweetClick

      Input Dialog will appear.
      tweet compose dialog interface in Android app

      TweetDialog

      Enter The Text in the dialogBox and click on Ok.
      tweet message input screen in Android app

      TweetMessage

      it will show the message the tweet is successful.
      successful tweet posting confirmation in Android app

      SuccessfulTweet

      The message will look something like this
      Twitter timeline update interface in Android app

      TwitterUpdate

    • Followers Screen : shows the list of followers.
      Twitter followers list interface in Android app

      FollowersList

    • Favorite Screen : shows the fovorite tweets.
      Twitter favorites list view in Android app

      FavoriteList