Displaying a progress dialog in Android Android 25.07.2016

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

android:text="Show Dialog"

Open MainActivity.java and add the following

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

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.


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


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;

protected void onCreate(Bundle savedInstanceState) {

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

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

    public LoadPage (Context context){
        ctx = context;

    protected void onPreExecute() {
        mDialog = new ProgressDialog(ctx);
        mDialog.setMessage("Doing something...");

    protected String doInBackground(String... urls) {
        int indx = 0;
        try {
            while (indx < 100) {
                indx += 10;
        } catch (Exception e) {
        return null;

    protected void onProgressUpdate(Integer... progress) {

    protected void onPostExecute(String result) {
        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


How to show an indeterminate ProgressBar


There are attributes

  • android:indeterminate Allows to enable the indeterminate mode.
  • android:indeterminateBehavior Defines how the indeterminate mode should behave when the progress reaches max.
  • android:indeterminateDrawable Drawable used for the indeterminate mode.
  • android:indeterminateDuration Duration of the indeterminate animation.
  • android:indeterminateOnly Restricts to ONLY indeterminate mode (state-keeping progress mode will not work).
  • android:indeterminateTint Tint to apply to the indeterminate progress indicator.
  • android:indeterminateTintMode Blending mode used to apply the indeterminate progress indicator tint.

Programmatically setting the progress bar indeterminate mode


Other methods related to progress bar indeterminate mode

  • setIndeterminateDrawable(Drawable d) Define the drawable used to draw the progress bar in indeterminate mode.
  • setIndeterminateDrawableTiled(Drawable d) Define the tileable drawable used to draw the progress bar in indeterminate mode.
  • setIndeterminateTintList(ColorStateList tint) Applies a tint to the indeterminate drawable.
  • setIndeterminateTintMode(PorterDuff.Mode tintMode) Specifies the blending mode used to apply the tint specified by
  • setIndeterminateTintList(ColorStateList) to the indeterminate drawable.

How to change color of ProgressBar

For Android 5.0 and above add following attributes to the layout


For lower version I use this


Useful links