Creating a Twitter Client for Android: Building the Interface

In this series we are building a Twitter client for the Android platform using the Twitter4J library. In this tutorial we will prepare our user interface elements for the app and handle signing the user into Twitter to allow access to their account.


Also available in this series:

  1. Creating a Twitter Client for Android: Setup & Overview
  2. Creating a Twitter Client for Android: Building the Interface
  3. Creating a Twitter Client for Android: Creating a Timeline Database
  4. Creating a Twitter Client for Android: Retrieving Updates Using a Service
  5. Creating a Twitter Client for Android: Tweeting, Retweeting, and Replying

Step 1: Authorize the App to Access the User's Twitter Account

Before we can access the user's Twitter account, we need them to sign in and grant the app permission. Open the main Activity class for your application, creating it now if Eclipse did not generate it automatically when you started the project. Alter your class declaration opening line as follows, and change the name of the class to suit your own if necessary:

The OnClickListener will detect various button clicks. You will need the following import statements added above your class declaration, although Eclipse may have added some automatically:

Inside the class, add the following instance variables:

Alter the Key and Secret variables to store the values you copied from your Twitter Developer account last time. Notice that the URL is an extended version of the data element we added to the Manifest, so make sure to alter it if you used a different value there. Add the following additional instance variables for connecting to Twitter:

When the user grants permission, we will add the resulting data to the Shared Preferences. Each time the app runs, it will check whether these details have been set. If so it will use them to fetch the user's timeline straight away. If the details have not been set it will prompt the user to sign in and grant permission. You will need the following import statements:

Find out if the user has already authorized the app

In the Activity onCreate method, after the call to the superclass method, add the following code:

Here we get a reference to the application Shared Preferences object to establish whether the user has already granted the app permission or not. The "user_token" preferences string is going to store the access token we use for accessing Twitter, so if it has already been set, we know the user has previously signed in for the app. If the preferences have not been set, we need to prompt the user to sign into Twitter, which we will do in the "if" statement - for the moment we simply set the main content view.

In the "else" block we take care of users who have already signed in and authorized the app. Here we call a helper method named "setupTimeline" which we will implement in the next tutorial.

Handle the First Run

Let's create our main layout, which we have set in the "if" statement. Normally Eclipse creates a file at "res/layout/main.xml" automatically, if not you can create it yourself by right-clicking the layout folder in the Package Explorer (or selecting it and then choosing the File menu), then choosing "New" and "File." Open the main layout file and enter the following XML code:

This is a LinearLayout with two text-fields and a button. The text-fields are simply informative, while the button will take the user to the Twitter sign-in Web page. The button has an ID attribute so that we can identify it in the Java code.


Now back to our onCreate method for the main app Activity to implement signing in first time users. You will need the following import:

Inside the "if" block, after the line in which we set the main content view, add the following:

Here we create an instance of the Twitter class from the Twitter4J library, which we already declared as an instance variable. This class is required for pretty much everything we do with Twitter. To verify our credentials, we pass the Key and Secret from the developer interface where the app was registered, for both of which we created class constants. Next we need to get a request token from the Twitter object so that we can attempt to authorize the app for the user's account:

We are here attempting to instantiate the request token for which we created an instance variable. The method can throw an exception, so we surround it in a try block and output an error message to the Android Log if the exception is thrown. Notice that the code uses the "LOG_TAG" constant we created - you can see the resulting messages in the Eclipse LogCat panel.

Send to Twitter

To finish the "if" block, set up a click listener for the sign-in button we included in our "main.xml" layout file:

The click listener for the button is the Activity class itself, so we now need to implement the onClick method anywhere in the class file:

The method uses a switch statement to tailor what happens to particular button presses. We will be adding a case statement for another button later, but for the moment all we need is the sign-in process. The code retrieves the authentication URL from the Twitter request token object, then opens the page in the Web browser.

Don't worry too much about how these Twitter4J methods are implemented. This is simply the process you need to follow if you use the API. Not having to concern yourself with the implementation details of connecting to Twitter is one of the main benefits to using an external library.

Return From Sign-In

We need to handle what will happen when the user is returned to the app after signing into Twitter to authorize it. When the user signs in successfully, Twitter will return them along with some data to verify and facilitate the app's access to their account. The "onNewIntent" method will fire, so let's implement it now:

The method retrieves the verification data from Twitter, which we will later use to access the user's tweets. So that we only need to carry out the sign-in process once, we store the required data in the application Shared Preferences. Still inside the "if" statement, add the following:

This is another method that can cause an exception to be thrown. The code inside the try block first retrieves the Access Token from Twitter using the data returned from the Web page. Then it uses the Shared Preferences object to store the Token and Secret necessary for Twitter access. Finally, the code calls the method to display the timeline, which we have not yet implemented.

If you want to test the sign in process just now, provide the "setupTimeline" method, simply including a message output to the Log, for example:

We will implement the method fully in the following tutorials.


Step 2: Create the Application Layouts

Now that we have handled signing users in the first time they run the application, we can turn to the user interface for the app as it runs subsequently. We will be using various resources in the user interface design for this app, including drawables, colors, and layouts. The layout XML files will refer to drawable resources which we will create in the next step. Don't worry if Eclipse alerts you to errors because the resources are not yet present. When you create the layout files, simply notice the references to resources you still have to create.

The app is going to use four layout files, one of which ("main.xml") we have already created for the sign-in screen which only appears on first run. We also need layouts to define the appearance of the main timeline, the Tweet screen and each update within the timeline. In each layout file, you will notice that many of the elements have ID attributes. This is so that we can refer to these elements within the Java application code, particularly when handling user interaction.

Timeline

The timeline layout is going to be the main interface users see when they launch the app, after authorizing it. The home timeline shows the list of recent tweets from those accounts the user follows. At the top of the screen, the user will see a header and the Tweet button, for switching to the Tweet screen to send a tweet. The content of each update in the home timeline will be defined in a separate layout file, but in the timeline layout we will take care of the container for the list of updates as a whole.

Create a new file in your "res/layout" folder, with "timeline.xml" as the file-name. Open the new file and add the following outline, which we will extend:

The View is defined by a LinearLayout. Inside this we have a TableLayout and a ListView. We will be adding elements inside the TableRow next. The ListView is going to hold the list of update tweets to display within the user's home timeline. The list will be populated with those tweets when the application executes. For the moment we simply define the View that will hold them.

Add content inside the TableLayout TableRow element:

This creates a header for the home timeline. The text "Home" is laid out next to a drawable resource named "home". Notice that the section also has a background which is another drawable resource named "homebg".

Next add the tweet button, still inside the TableRow but after the LinearLayout you just added:

As you can see, this is similar to the Home header, but in this case the View is going to function as a button - the LinearLayout has its "clickable" attribute set to true. The button will include the text "Tweet" displayed next to an image. When users click this button they will be taken to the Tweet screen.

At the moment when you choose the Graphical Layout tab for your timeline XML in Eclipse, you won't see much. Once we have created the drawables and the update layout file, it will look like this:


Since the content of the timeline is only built at runtime, you cannot really see what the interface will look like until you are able to run the app.

Update

We are going to use a layout file to model a single update within the home timeline. Create a new file in your layout directory, naming it "update.xml". Enter the following outline:

The layout contains a section for the profile image of the account whose tweet is being displayed, then a section for the tweet content. Inside the second LinearLayout element we will add the tweet content and the buttons for retweeting/replying. Start with the tweet content:

Each tweet includes the screen name of the tweeting account, the time the tweet was sent, and the tweet itself. Each TextView has an ID attribute, as the Java code is going to map the incoming tweet data to these Views when the app runs. The final TextView, for the tweet text itself, has the "autoLink" attribute set so that users can click links in the tweets. The user screen name view is also clickable, so that users can browse to the profile page of each account in their timeline. Notice that these Views also include references to color resources we have not yet created.

Now let's add the retweet and reply buttons, after the TextView we just added:

Every tweet in the timeline is going to be accompanied by these buttons, each with a little text and a background. Here is a snapshot of a single update in the finished app.


Tweet

Now we turn to the other main screen in our app, the Tweet screen. Create another new file in your Layout folder, naming it "tweet.xml". Enter the following outline:

We will add more elements after the TableLayout next. Don't worry about adding all of this code at once - if you compare it to the timeline layout above you'll see that it's almost identical. The top section with the Home header and Tweet button will appear similarly in both the home timeline and Tweet screens. Apart from some small cosmetic differences to distinguish the two, the main difference is that in the Tweet screen, the Home header functions as a button to take the user back to their home timeline, whereas in the timeline layout, the Tweet View functions as a button.

After the TableLayout, add the following:

This is the main content for the Tweet screen. A small amount of informative text precedes the editable text-field for the user to enter their tweet text, then we have a button for them to go ahead and send the tweet. Once we create our drawable resources, in the Graphical Layout tab for the tweet layout you will see something like this:



Step 3: Create the Application Drawables

Now let's create the visual elements in our interface, most of which we have already referred to in our layout resources. Some of our drawables will be defined in XML files and some will be created outside the application, using a graphic design or image editing program. You can of course alter any of the visual elements you like, but I would recommend starting with the steps outlined in this tutorial, then making changes once you understand how the various ingredients relate to one another.

Images

For this application we are using three images created outside Eclipse, for the app icon, the home timeline, and the tweet button. You can create your own versions of these if you like, or not bother using images at all if you prefer. The home timeline is a simple drawing, while the tweet button and app icon use the official Twitter resources, which also include retweet, reply and favorite icons.

When you have your drawables ready, copy them into your application workspace folder, in "res/drawables-*". Eclipse should have created three drawables folders in your project workspace, for low, medium, and high resolution devices. The images must be saved as "ic_launcher" for the icon, "home" and "tweet" for the buttons. Make sure each image is saved with the same name in each folder, e.g. the tweet image could be named "tweet.png" in all three folders, although the images in each folder are actually different sizes, to suit the different resolutions.

You may need to instruct Eclipse to refresh your workspace before your code can refer to the new images successfully. To do so, select the project in the Package Explorer, right-click or choose File, then Refresh.

Colors

Other than black, white, and gray, the app interface mainly uses two colors, shades of green and blue. Let's define these as color resources. In Eclipse, create a new file in the "res/values" directory named "colors.xml":


In the colors XML file, enter the following code to define the two main colors in the app:

This code indicates two colors plus opaque versions of each. Now if you want to change the color scheme for the application buttons, you can do it in a single location.

Backgrounds

Finally, let's get our backgrounds created to complete the user interface design. We will run through each drawable file in turn, but remember that you need to copy every one into all three drawable folders in your app's resources directory.

Create a new file in your drawables folder(s) named "homebg.xml" with the following content:

This defines a simple drawable shape with padding and a gradient fill. If you look back at your timeline XML layout code, you will see this drawable referred to as the background for a LinearLayout.

Create another new file named "homebtnbg.xml" in your drawables with the following content:

This background is used for the Home button in the Tweet screen. Next create a file named "profilebg.xml" with the following content:

This time we define rounded corners as well as padding and gradient fill. This background is for displaying behind the profile images in the timeline.

The final two drawables are slightly more complex. For the Tweet button and the buttons in each update (for replying or retweeting) we are going to define different appearances for selection states so that the buttons change in appearance when the user presses them. Create a file named "tweetbtnbg.xml" with the following content:

This code defines two slightly different shapes, one for when the button is pressed. The only difference between them is in the colors, which are defined as the color resources we created. Finally, create a similar file named "updatebtnbg.xml" with the following content:

Once you have these copied into each of your drawables folders, you can choose to alter them to suit different screen sizes if you wish. Your drawable resources should look something like this, with all files in all three folders:


Eclipse should now stop displaying error messages for your layout files, as all referenced resources are present.


Next

Now our user interface design is complete! In the next tutorial we will create a database to store the update tweets, then use an Adapter to map these updates to the View elements so that the user can see them.

Tags:

Comments

Related Articles