How to create ProgressDialog in android?

Android Progress Dialog is an extension of the AlertDialog class that can display a spinner wheel or a progress bar. These dialogs help us to inform the users about the time consuming tasks and keep them involved till they get their work done. Android progress dialog can be of two types.

  1. ProgressDialog with a horizontal progress bar.
  2. ProgressDialog with a ciruclar, spinning progress bar.

A ProgressDialog with a Spinner wheel looks like the below screen.

A ProgressDialog with the horizontal Progress Bar is as below.


These ProgressDialogs can have buttons too(Example: to cancel a download progress). The dialog can be made cancelable on back key press.

This tutorial demostrates how you can create a simple ProgressDialog with the Spinning Wheel.

Creating a ProgressDialog with circular spinning wheel

The Spinning wheel ProgressDialog  is often used when your long running task takes undefined time or its time of completion is not known.

A progress dialog can be opened by calling This can be easily done without managing the dialog inside onCreateDialog() callback as used in the post AlertDialog while creating an AlertDialog which has lists,Checkboxes and RadioButtons.

The syntax of is as below.,
"Please wait", "Your result is being calculated", true);

where the first parameter represents the context, second represents the title, third is the message and the last parameter tells whether the ProgressDialod is indeterminate or not. We have passed true since we are creating a spinning wheel ProgressDialog. The default style of the ProgressDialog is Spinning Wheel. Hence you can use the show() method without the last parameter.

Let us now discuss a simple example which explains the steps to create a Spinning wheel Progress Dialog.

In this example we are building a result calculation system. When the user clicks on the submit button, a ProgressDialog is shown, which asks him to wait till the result is being calculated. Then the result is shown to the user. Here the emphasis is more on creating a ProgressDialog and not on result calculation. Hence the “score” String is hardcoded with some value to display it to the user.

Step 1: Set up the android working environment

If you are not familiar with setting up an android environment, please have a look at the post, Environment. This example is built using API level 8.

Step 2: Create an Android Project

Create an android project named ProgressDialogDemo with the launcher activity If you are not familiar with the android project creation, please refer to the post Simple android project.

Step 3: Create the required layouts

In this example, We would be using main.xml layout file. If you want to know more about layout files, please refer to the post Layouts. Open the main.xml file and paste the below code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="<a href=""></a>">
&lt;TextView android:id=&quot;@+id/TextView01&quot; android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot; android:text=&quot;Press Submit button to get your results&quot;
android:layout_centerHorizontal=&quot;true&quot; android:textStyle=&quot;bold&quot;
android:textSize=&quot;15sp&quot; android:layout_marginTop=&quot;30dip&quot;&gt;&lt;/TextView&gt;

&lt;Button android:id=&quot;@+id/Button01&quot; android:layout_below=&quot;@id/TextView01&quot;
android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot; android:text=&quot;Submit&quot;

&lt;TextView android:id=&quot;@+id/TextView02&quot; android:layout_below=&quot;@id/Button01&quot;
android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot; android:visibility=&quot;gone&quot;
android:textSize=&quot;16sp&quot; android:textStyle=&quot;bold&quot;
android:text=&quot;congratulations!! Your score is &quot;&gt;&lt;/TextView&gt;

The main.xml has a “Submit” button which triggers a ProgressDialog. It has 2 textviews.The first one is Textview01, which tells the user to click “Submit” button. Textview02 is made invisible in the beginning.

Step 4: Create the required Activities

Here in this example, we would be using launcher activity Open and paste the below code.


import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActivityProgressDialog extends Activity {
ProgressDialog progressdialog;
Button Spinner_progress, Progress_bar;
ProgressDialog progressbar;
Handler mHandler;
TextView instruction,marks_update;
String score;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

Spinner_progress.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub,
&quot;Please wait&quot;, &quot;Your score is being calculated&quot;);

//creating a separate thread
new Thread(new Runnable() {

public void run() {
// TODO Auto-generated method stub

try {
} catch (InterruptedException e) {
// TODO Auto-generated catch block

mHandler = new Handler()
public void handleMessage(android.os.Message msg)
switch (msg.what)
case 10:


private void getResults() {
// TODO Auto-generated method stub
try {
/*Write the code of your long
*running tasks here*/

//assigned 80% to score string, to display the results


} catch (Exception e) {
// TODO: handle exception


Let us now try to understand the flow of the above code. In the onCreate() function we create the ProgressDialog on click of the button “submit” using the method As you can notice, we have created a separate thread to calculate the result. when the thread.start() is executed, it creates a new thread and executes the run() function, which inturn calls the method getResults(). Before calling getResults() I have made the thread to sleep for some time. This is done to make the ProgressDialog visible for more time.

The getResults() method calculates your score(does long running tasks in other case) and sends an Empty message to the handler. Sometimes its difficult to update the UI when you are running separate threads hence here we use handlers to update the UI. When the handler receives the message, it dismisses the ProgressDialog and does the UI updations.

Step 5: Declare the activities in AndroidManifest.xml

Your launcher activity is declared automatically. If you want to know more about AndroidManifest.xml file, please refer to our earlier post, Manifest.

Your Manifest file has the below code.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;&lt;a href=&quot;;&gt;</a>"
&lt;application android:icon=&quot;@drawable/icon&quot; android:label=&quot;@string/app_name&quot;&gt;
&lt;activity android:name=&quot;.ActivityProgressDialog&quot;
&lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;

&lt;uses-sdk android:minSdkVersion=&quot;8&quot; /&gt;


Step 6: Run your project

Select your project->right click->Run As->Android Application.

Your project runs and the activity fires up as below.


Press the submit button to see your progress Dialog.


Then the score displays as below.

progressdialog_outSo far we have seen how to develop an android Spinning Wheel ProgressDialog to make the users involved when a long running tasks need to be done in the background. We would be studying about Horizontal Progress Bar( the other type of ProgressDialog) in our upcomming posts. I Hope this tutorial served your purpose. Feel free to post your comments if you have any queries.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!