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

      add_jar1

      add_jar2

    • Import all files destination group folder. It should also look as shown below

      ImportedLib

      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>
      

      Code Description :
      Login:

      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();
      }
      }
      

      Tweet Button Click :

      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();
      }
      

      Friends Button Click :

      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));
      

      Followers Button Click :

      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));
      

      Favorite Button Click :

      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));
      

       

    • Now Save project (Command +S). Build and Run Project.

    Simulator will look like as follows

    • Login Screen : enter your twitter user name and password

      LoginScreen

    • Friends Screen : shows your friends list.

      FriendsList

    • Tweet : Click on Tweet Button

      TweetClick

      Input Dialog will appear.

      TweetDialog

      Enter The Text in the dialogBox and click on Ok.

      TweetMessage

      it will show the message the tweet is successful.

      SuccessfulTweet

      The message will look something like this

      TwitterUpdate

    • Followers Screen : shows the list of followers.

      FollowersList

    • Favorite Screen : shows the fovorite tweets.

      FavoriteList