How to Add a Progress Dialog to your Android Application

How to Add a Progress Dialog to your Android Application
Page content

In this tutorial, I will show you how to add a progress dialog to your Android application. A dialog is usually a small window or tab that appears before any activity in your application. It is intended to display alerts or information to the user. A progress dialog can be used to either show some processing activity to the user or to show the progress of some custom activity using the progress bar. It can be used in many situations, like to display the size of the files being downloaded and to show the amount of time left to finish a certain activity etc.

To create a Progress Dialog, we can make use of the ProgressDialog class which is an extension of the AlertDialog class. It can be used to display a standard progress dialog with a spinning wheel and some custom text. You can display your Progress Dialog using a simple show () call on your dialog object.

Here’s the syntax to create a standard Progress dialog:

ProgressDialog MyDialog = ProgressDialog.show( MyActivity.this, " " , " Loading. Please wait … “, true);

It has four parameters:

1. The application context

2. The title of the Dialog

3. The Dialog Text or Message which is displayed in the dialog

4. A boolean value indicating whether the progress is indeterminate

This will just display a spinning wheel animation alongwith the text.

To create a Progress bar, create a new Progress Dialog and initialize it using ProgressDialog( Context ).

ProgressDialog pbarDialog;

pbarDialog = new ProgressDialog( mContext );

After that, just set the Progress Style to STYLE_HORIZONTAL,

pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

Also enter the dialog message text to whatever you want and whether it is cancelable or not.

pbarDialog.setMessage(“Loading…”);

pbarDialog.setCancelable(false);

Now, if you want to increment or edit the progress displayed on the progress bar from your application, just call incrementProgressBy(int) or setProgress(int) on pbarDialog with an appropriate int value.

Here is how the progress bar or progress dialogs will actually look like in your application.

progress bar