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.
- ProgressDialog with a horizontal progress bar.
- ProgressDialog with a ciruclar, spinning progress bar.
A ProgressDialog with a Spinner wheel looks like the below screen.
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 ProgressDialog.show(). 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 ProgressDialog.show() is as below.
ProgressDialog.show(ActivityProgressDialog.this, "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 ActivityProgressDialog.java. 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.
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;RelativeLayout android:id=&quot;@+id/RelativeLayout01&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; xmlns:android=&quot;&lt;a href=&quot;http://schemas.android.com/apk/res/android&quot;&gt; http://schemas.android.com/apk/res/android</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; android:layout_marginTop=&quot;15dip&quot;&gt;&lt;/Button&gt; &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; &lt;/RelativeLayout&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 ActivityProgressDialog.java. Open ActivityProgressDialog.java and paste the below code.
package my.app; import android.app.Activity; import android.app.ProgressDialog; 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. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Spinner_progress=(Button)findViewById(R.id.Button01); instruction=(TextView)findViewById(R.id.TextView01); marks_update=(TextView)findViewById(R.id.TextView02); Spinner_progress.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub progressdialog=ProgressDialog.show(ActivityProgressDialog.this, &quot;Please wait&quot;, &quot;Your score is being calculated&quot;); //creating a separate thread new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(4000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } getResults(); } }).start(); mHandler = new Handler() { public void handleMessage(android.os.Message msg) { super.handleMessage(msg); switch (msg.what) { case 10: progressdialog.dismiss(); marks_update.setVisibility(View.VISIBLE); marks_update.append(score); instruction.setVisibility(View.GONE); Spinner_progress.setVisibility(View.GONE); break; } } }; } }); } 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 score=&quot;80%&quot;; mHandler.sendEmptyMessage(10); } catch (Exception e) { // TODO: handle exception mHandler.sendEmptyMessage(1); } } }
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 ProgressDialog.show(). 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;http://schemas.android.com/apk/res/android&quot;&gt; http://schemas.android.com/apk/res/android</a>" package=&quot;my.app&quot; android:versionCode=&quot;1&quot; android:versionName=&quot;1.0&quot;&gt; &lt;application android:icon=&quot;@drawable/icon&quot; android:label=&quot;@string/app_name&quot;&gt; &lt;activity android:name=&quot;.ActivityProgressDialog&quot; android:label=&quot;@string/app_name&quot;&gt; &lt;intent-filter&gt; &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt; &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;/application&gt; &lt;uses-sdk android:minSdkVersion=&quot;8&quot; /&gt; &lt;/manifest&gt;
Step 6: Run your project
Select your project->right click->Run As->Android Application.
Press the submit button to see your progress Dialog.
Then the score displays as below.