Displaying a progress dialog in Android

ProgressDialog are used to show progress of a task. For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.

Android ProgressDialog is an extension of AlertDialog and has been available since API 1. It's simple to use, but keep this statement in mind (from the Android Dialog Guidelines site):

Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. However, if you need to indicate loading or indeterminate progress, you should instead follow the design guidelines for Progress & Activity and use a ProgressBar in your layout. Source

This message doesn't mean the ProgressDialog is deprecated or is bad code. It's suggesting that the use of the ProgressDialog should be avoided, since the user cannot interact with your app while the dialog is displayed. If possible, use a layout that includes a progress bar, instead of using a ProgressDialog (like in Google Play).

Some important attributes of android ProgressDialog are given below.

  • setMessage() method is used to show the message to the user.
  • setTitle() method is used to set a title to the dialog box.
  • setProgressStyle(ProgressDialog.STYLE_HORIZONTAL) method is used to show the horizontal progress bar in the dialog box.
  • setProgressStyle(ProgressDialog.STYLE_SPINNER) method is used to show the circle/spinning progress bar in the dialog box.
  • setMax() method is used to set the maximum value.
  • getProgress() method is used to get the current progress value in numbers.
  • getMax() method returns the maximum value of the progress.
  • show(Context context, CharSequence title, CharSequence message) method is a static method, used to display progress dialog.
  • incrementProgressBy(int diff) method increments the progress bar by the difference of value passed as a parameter.

I will demonstrate both the Ring and the Bar style of the ProgressDialog component.

Indeterminate dialog indicator

Since this is just a demonstration on using the ProgressDialog, we will create a button to show the dialog. To simulate some long task, we will use a delayed message to dismiss the dialog. To start, open activity_main.xml and add following snippet

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="showProgress"/>

Open MainActivity.java and add the following

public void showProgress(View view) {
    final int THREE_SECONDS = 3*1000;
    fianl ProgressDialog dlg = new ProgressDialog(this);
    dlg.setMessage("Doing something...");
    dlg.setCancelable(false);
    dlg.show();
    new Handler().postDelayed(new Runnable() {
        public void run() {
            dlg.dismiss();
        }
    }, THREE_SECONDS);
}

Normally, a dialog can be cancelled using the back key, but when setCancelable is set to false, the user is stuck on the dialog until it is hidden/dismissed from the code.

android_pd_indeterminate.png

Determinate dialog indicator

We used the default ProgressDialog settings in previous example, which creates an indeterminate dialog indicator, for example, the continuously spinning circle. If you can measure the task at hand, such as loading files, you can use a determinate style instead

dlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

I'm going to show how to use AsyncTask and ProgressDialog in union for long running task. Add following snippet to MainActivity

private ProgressDialog mDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void showProgress(View view) {
    LoadPage task = new LoadPage(this);
    String[] urls = {"http://proft.me"};
    task.execute(urls);
}

private class LoadPage extends AsyncTask<String, Integer, String> {
    private Context ctx;

    public LoadPage (Context context){
        ctx = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mDialog = new ProgressDialog(ctx);
        mDialog.setMessage("Doing something...");
        mDialog.setCancelable(false);
        mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mDialog.show();
    }

    @Override
    protected String doInBackground(String... urls) {
        int indx = 0;
        try {
            while (indx < 100) {
                indx += 10;
                publishProgress(indx);
                Thread.sleep(200);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        mDialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String result) {
        mDialog.dismiss();
        Toast.makeText(MainActivity.this, "Page loaded", Toast.LENGTH_SHORT).show();
    }
}

You will get the following dialog style as an output to the preceding line of code

android_pd_determinate.png
comments powered by Disqus