Android: Progress dialog in a ListActivity using ListView
I woke up today morning all charged up to work on my app again (barely slept yesterday). The fist thing I wanted to do was to add a progress dialog to my applications list activity. The activity isn’t time consuming (not much anyway) but you don’t want the UI to be unresponsive, gives a bad impression to the user.
So while the UI thread is busy on my onCreate doing stuff for me I want to show a loading dialog box. Easy right? Not really. Unless you’re an android expert (and I am not) there are a lot of pitfalls. There are things you need to work around. For example, displaying the dialog box in the UI thread, performing the activity in it and then hiding the box doesn’t work and isn’t a good idea anyway.
You must update the main UI from a separate thread. To do this I had to spawn another thread in my main onCreate method. While this does not seem ideal to me at all (I hate working with threads directly), I’m going to do this for the purposes of this example anyways.
Note: I couldn’t get the default progress dialogs to work, so we will create our own in this example. Once I get the default dislogs to work, I will put up another article.
Note2: My default activity extends ListActivity.
First, some code to create the ProgressDialog
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setMax(100);
dialog.show();
Remeber to create the dialog before you do anything else with the UI like register for context menu create etc. If you don’t then you will get an exception.
Once we’re done with the dialog box, we go ahead and spawn our worker thread.
Thread myThread = new Thread(new Runnable() {
public void run() {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.setProgress(100);
dialog.dismiss();
}
});
myThread.start();
See the line setting the progress to 100. This is the max progress we hve to set. If you don’t set the progress to a value equal to the max progress then the dialog box won’t go away even if you call dismiss() on it. I spent a good half hour wondering why the bar wouldn’t go away because of this.
And that’s it, your dialog box is ready. The full activity code looks like this:
public class HelloAndroid extends ListActivity {
private List<PackageInfo> packages = new ArrayList<PackageInfo>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setCancelable(true);
dialog.setMessage("Loading...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(0);
dialog.setMax(100);
dialog.show();
registerForContextMenu(getListView());
Thread myThread = new Thread(new Runnable() {
public void run() {
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.setProgress(100);
dialog.dismiss();
}
});
myThread.start();
setListAdapter(new MyAdapter(this, packages));
}








