Advertisement
Android SDK

Creating a Twitter Client for Android - Tuts+ Premium

by

It goes without saying that many people use mobile devices to connect to social media services. In this series of tutorials, we will create a basic Twitter client for the Android platform. We will use the Twitter4J library to connect to Twitter. The app will display the user’s home timeline, send tweets, retweet, and reply to tweets.


Tutorial Teaser

Step 1: Create a Tweet Activity

Our app is going to have a second Activity in addition to the main timeline screen. This new Activity is for sending Tweets. Inside it, the user can enter text to send a tweet from their Twitter account. The user may enter the Tweet Activity in two ways - by pressing the Tweet button on the app's main screen or by pressing a reply button within the timeline.

The Main App Screen

If the user presses the Tweet button, they will be presented with an empty text-field for entering their tweet, with a send button to go ahead and send it.

The Tweet Screen

If the user presses the reply button within a timeline tweet, they will also be presented with the text-field, but the reply-to username will already be populated within the field. The reply must also be sent using the ID of the tweet being replied to, which we will implement as well.

Create a new class in your Android project, naming it "NiceTweet" to match the name you included in your Manifest file in the first tutorial. Alter the class declaration as follows:

public class NiceTweet extends Activity implements OnClickListener {

You will need the following Android imports:

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

Plus these Twitter4J imports:

import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;

Include the following instance variables within your class declaration:

	/**shared preferences for user twitter details*/
private SharedPreferences tweetPrefs;
	/**twitter object**/
private Twitter tweetTwitter;

	/**twitter key*/
public final static String TWIT_KEY = "your key";
	/**twitter secret*/
public final static String TWIT_SECRET = "your secret";

	/**the update ID for this tweet if it is a reply*/
private long tweetID = 0;
	/**the username for the tweet if it is a reply*/
private String tweetName = "";

We will use the Preferences, Twitter, key and secret variables to access the Twitter4J methods for tweeting from the user account. Alter the key and secret to suit your own. The final two variables are for replies. When the user presses the reply button within a tweet, we are going to pass the ID of the tweet being replied to, together with the Twitter screen-name of the account we are replying to. We will store these in the two instance variables.

Let's implement the Activity create method:

/*
 * onCreate called when activity is created
 */
@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		//set tweet layout
	setContentView(R.layout.tweet);  
}

All we do here is set the tweet layout we defined in XML earlier. Next implement the resume method, which is called whenever the Activity becomes visible to the user:

/*
 * Call setup method when this activity starts
 */
@Override
public void onResume() {
	super.onResume();
		//call helper method
	setupTweet();
}

We are going to provide the specified helper method to set the Activity up for user interaction.


Get the Full Series!

This tutorial series is available to Tuts+ Premium members only. Read a preview of this tutorial on the Tuts+ Premium web site or login to Tuts+ Premium to access the full content.


Joining Tuts+ Premium. . .

For those unfamiliar, the family of Tuts+ sites runs a premium membership service called Tuts+ Premium. For $19 per month, you gain access to exclusive premium tutorials, screencasts, and freebies from Mobiletuts+, Nettuts+, Aetuts+, Audiotuts+, Vectortuts+, and CgTuts+. You'll learn from some of the best minds in the business. Become a premium member to access this tutorial, as well as hundreds of other advanced tutorials and screencasts.

Related Posts