Android Development Tutorial: ProgressBar Example
The Android ProgressBar is a useful UI component that most developers will quickly find need of. Displaying progress, even an indeterminate "loading" indicator, provides crucial feedback to a user, eliminating frustration and confusion while your app is churning away in the background.
Today I'll demonstrate using an indeterminate ProgressBar within a ListView while loading web content. I'll be modifying the TweetView project from my previous post.
Note: You can find the full project with the ProgressBar feature in the "progress_bar" branch of the TweetView Github project.
Let's get started. Recall that the previous iteration of the TweetView app displayed a stock Android icon image by default while the Twitter avatars were loaded for each item in the ListView. Of course, this is not ideal, as it gives the user no indication of what is going on, and new images suddenly replacing the icon may be confusing. A better user experience would be to place a loading image in each ListView item until the proper avatar is retrieved and displayed. To avoid the complication of trying determine progress of an image download (especially since our avatar images are not very large, and will download fairly quickly), I'll use an indeterminate ProgressBar:
Step 1: Insert a ProgressBar UI component into our listitem.xml layout file.
We'll put the ProgressBar element in the same spot we intend the avatar to occupy. This way, we can just set the visibility of the avatar ImageView to "gone" until the image is ready, and then switch the ImageView to "visible" and the ProgressBar to "gone" when we display the image. Here's the updated layout file:
You'll see that i've added something more than a ProgressBar element: a RelativeLayout. If we don't set the ProgressBar's height to fill_parent, it will squish our list items vertically to match it's height. However, the ProgressBar element, unlike an ImageView, will stretch and distort if told to "fill_parent" in its layout_height or layout_width value. to avoid distortion of the indeterminate ProgressBar or the hiding of the text from our tweets, I've added the RelativeLayout to encapsulate ProgressBar and ImageView alike, providing the ability to stretch vertically and buffer our round ProgressBar with dead space.
Note also that I've set the sizes of the ProgressBar to match the size of the avatars we download from Twitter. You will want to change these size settings as appropriate for your app. This, combined with the layout hack above, will make our tweet items have a consistent layout, whether we are displaying avatars or a ProgressBar.
Step 2: TweetImageAdapter and our ViewHolder class need to be altered to store the ProgressBar for each list item, and pass it to the ImageManager.displayImage() method so it can be made invisible when the correct image is displayed.
First we add a ProgressBar member to the ViewHolder class:
Then we alter the tweetItemAdapter.getView() method to retrieve and pass along this ProgressBar object to the ImageManager through the displayImage() method:
Ok, now our ImageManager, the class responsible for updating the image displayed when an avatar has finished downloading, has access to the ProgressBar component as well as the ImageView. We're ready for the last step.
Take my online course featuring screencasts and sample projects!
Step 3: Change the image display methods to manage visibility of the ImageView and ProgressBar list item components, as well as to set the appropriate source image for the ImageView. Recall that we had two methods capable of displaying an image, one that could do so immediately (if the image was found in our local cache) and one that operated asynchronously, displaying the image as soon as the background download process is complete.
The first method, displayImage(), can be altered like so:
Notice that the ProgressBar object is now being passed in, as designed in the last step. Using this, we are able to set visibility of the ImageView/ProgressBar UI elements (making sure only one is visible at any given time), depending on whether the image bitmap has been set or not.
Additionally, we pass the progressBar object along into the queueImage method - more on that soon.
The second place we need to add this visibility shuffle code is the run() method of the Runnable BitmapDisplayer class, which will be invoked on the UI thread, from the background thread, to set the image. Same basic changes - the BitmapDisplayer class now looks like:
Step 5: Last, but not least, you'll notice above that the BitmapDisplayer object needs to have a ProgressBar object passed in as an input parameter. The BitmapDisplayer object in TweetView is populated by data from ImageRef objects, so ImageRef objects now need to contain this additional object. Let's take care of that, and while we are at it, change the ImageQueueManager run() method to get the BitmapDisplayer the updated input parameters that it needs:
All done! Now we will see a lovely perpetually spinning progress wheel, sure to delight users while they wait for Twitter avatars to be downloaded. Best of all, through some simple layout modifications, we should see no distortion or inconsistency in our item layouts, leading to a pleasant user experience.
I've created a new branch of the TweetView Github project containing these modifications. You can find it here: progress_bar branch. Just switch to this branch to see this version of the app.
As always, comments or questions are very welcome. Happy coding!
Technorati code: 7ESRQSJMX8BM