How To Use Progress Dialog Box In Android


Progress dialog box is mostly used component in all User Interfaces, when you want to display the progress of a task that is taking up some time. In this tutorial I am explaining how to show Progress Dialog Box in android to let the user know the progress. In this example we will use Thread to process the task in background and after completion of the task progress dialog will be closed.

Create Project:
Open eclipse IDE and create an android application project with name ProgressDialogBox and the package name com.tricksofit.progressdialogbox. Create a class MainActivity as main intent to start Activity and main layout activity_main.
Our manifest file will look like as below.

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tricksofit.progressdialogbox"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.tricksofit.progressdialogbox.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Main Layout:
Open main layout file and paste the below code.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ProgressDialog" />

</LinearLayout>

When you paste above code you may open graphical layout editor to preview User Interface.

progress dialog box user interface
progress dialog box user interface

Main Activity:
Now we have to write the code of the application to show the progress dialog box using ProgressDialog class. Now assume the task will take around some seconds and to do the task we will use thread. It takes the runnable object and process the task and after completion of the task progress dialog will dismiss.

MainActivity.java
package com.tricksofit.progressdialogbox;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	ProgressDialog progressDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button progrssBtn = (Button) findViewById(R.id.button1);
		
		progrssBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				progressDialog = ProgressDialog.show(MainActivity.this, "Please wait ...",	"Task in progress ...", true);
				progressDialog.setCancelable(true);
				new Thread(new Runnable() {
					@Override
					public void run() {
						try {
							//Do some stuff that take some time...
							Thread.sleep(3000); // Let's wait for some time
						} catch (Exception e) {
							
						}
						progressDialog.dismiss();
					}
				}).start();
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

Run Application:
When you click ProgressDialog button on application you will see the dialog box will pop up with the message “Task in progress …”.

Progress Dialog Box In Android
Progress Dialog Box In Android

Progress Dialog Box in Bar style:
To show the Progress Dialog Box in Bar style need to use of some special tools like Handler to update the value of the bar format. For example we have a task that need some seconds to complete so after every interval we will update the progress using Handler and Runnable.

So for Bar style progress dialog box our Main Activity code will be as below:

MainActivity.java

package com.tricksofit.progressdialogbox;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

	ProgressDialog progressDialog;
	Handler _handler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		_handler = new Handler();
		
		Button progrssBtn = (Button) findViewById(R.id.button1);
		progrssBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				progressBarDialog();
			}
		});
		
	}

	
	public void progressBarDialog() {
		progressDialog = new ProgressDialog(MainActivity.this);

		progressDialog.setTitle("Please wait ...");
		progressDialog.setMessage("Task in progress ...");
		progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
		progressDialog.setProgress(0);
		progressDialog.setMax(10);
		progressDialog.show();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {

					// some stuff to do the task...
					while (progressDialog.getProgress() <= progressDialog.getMax()) {

						Thread.sleep(1000);

						_handler.post(new Runnable() {
                            public void run() {
                            	progressDialog.incrementProgressBy(1);
                              }
                          });

						if (progressDialog.getProgress() == progressDialog.getMax()) {
							progressDialog.dismiss();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
}

DOWNLOAD CODE

And when you run the application the progress dialog box will look like as below:

Progress Dialog Box Bar Style In Android
Progress Dialog Box Bar Style In Android
  • Deepak

    hi, good job, easy to understand.
    keep it up.

    Deepak Swami

  • Deepak

    You should correct the flow of tutorial.
    then it would be good for beginners.

    • Thank you deepak for the meaningful suggestion I will keep it in mind for next tutorials.