Last week I demonstrated designing and implementing customized layouts for Android ListView items, including some simple example code. This time around I'd like to expand on that code to make something a little closer to an actual app, illustrating some new tools and concepts in the process such as working with JSON data and the Android BitmapFactory.
In this post I'll show you how to implement a Twitter feed reader for Android. Let's start by familiarizing ourselves with the Twitter Search API Say we wanted to see what Twitter users have to say about Android today. A Twitter API search query would look something like:
Twitter's Search API allows up to 100 results to be returned per query, which is more than enough for our example today, but which introduces some interesting challenges as we want to display more data in a user-friendly fashion - more on that later. For today's purposes, I'll restrict return data to 25 results, using the following query:
The "page" parameter can be incremented to step through chunks of 25 results in sequential queries. Go ahead and click on that link (will open a new window/tab) to check out the JSON data returned. There's a lot of data values in there, but for simplicity we'll be focusing on the following values:
I'll expand the simple example app from my last post to display twitter posts instead of a hard-coded list of users. The layout from the previous tutorial can be easily repurposed by changing the icon to the Twitter user's avatar, placing the twitter username in the "username" field, and placing the tweet in the message field. Our goal is something like:
So how do we get there? Let's go through it, step by step. First, we edit the listitem.xml file to give the View components more helpful names/IDs:
We can keep the main.xml from the previous post, since all we are changing is the layout within each item. Now we need some workhorse code to manage calling the Twitter API and formatting the return data for easy use. Let's wrap our returned tweets in a simple Tweet class:
I've also written a simple getTweets(searchTerm, page) method to generate the API call and process the return data into our Tweet objects. Here goes:
The code above is using the json-simple library, a nice lightweight tool for JSON encoding and decoding. I don't have time now, but I may do some demos/examples using json-simple in a later post if there is interest. Aside from exception handling, all the code above does is formats the Twitter search request url from the input parameters "searchTerm" and "page", makes an HTTP GET request using the generate url, parses the JSON return value, and stuffs the data into Tweet objects.
Notice that the Twitter user avatar data (profile_image_url) returned is simply a url to the user's avatar. We need one more processing step to get the actual image, since Android ImageViews can't display an image directly from the web without downloading it. (good thing too, or offline usage would be quite dull!) Let's see how few lines we can do this in:
May not break any records, but the code above is fairly compact. It uses another handy Android API tool called the BitmapFactory. The BitmapFactory lives and breathes bitmaps, its sole purpose in life being to return a bitmap from whatever source data you give it. File, filestream, byte array, you name it. Because of this, I don't have to put any thought at all into bits, bytes, files, http requests, or anything else to accomplish this task. Rock on, BitmapFactory.
Now we have all of the tools we need to refactor the ItemAdapter from last week's post to display our Tweet objects. Here we go:
Not too many changes from the original, but the changes we have made are important. Let's go through them. No conceptual changes in the constructor, so I'll skip that. getView() is where the magic happens. Now, since we are setting the avatar image independently for each user, we are grabbing an ImageView object as well as two TextView objects from our View. Since we already parsed the Twitter JSON data in the getTweets() method and packaged it nicely into our Tweet objects, all we need to do here is put the extracted values from the relevant Tweet object into the text field for our TextView objects. For the ImageView, we need to set a bitmap value, which is exactly what we wrote the getBitmap(url) method to return. That's it!
For the sake of completeness, here is the onCreate() method for this example - this illustrates setting the adapter and passing some data to the ListView as our app is launched.
For this example, we only ever get a single API call's worth of data, a limitation I will address in a follow-up tutorial. Let's take a screenshot of the finished product for posterity:
There you have it: a working Twitter feed reader, based on the Twitter search API. There are plenty of directions you could take this code, from allowing the user to specify search terms to changing the way data is aggregated or adding some persistent data features. If these are the types of things you are interested in, I hope this tutorial helps! Feel free to leave a comment or drop me a line if you have any questions. As always, you can download code using the links at the bottom of this post to try things out for yourself.
In my next post I'll continue working on this Twitter app to add user scrolling detection and auto-expansion of the ListView, so we can continually add to the ListView as the user scrolls through. This is a handy trick that can be useful in many scenarios. Stay tuned!
Example.java listitem.xml main.xml
Get 50% off my Node.js course here
If you liked this article, help me out by sharing a 50% discount to my Node.js course here: Tweet Thanks!
You should follow me on Twitter here: Follow @AaronCois