How To Display A Progress Dialog Window In Android

Overview 

In this article, I will explain about displaying a progress dialog window.To create an environment for Android application development, get an idea about activities life cycle and dialog window, refer the articles, stated below:
  1. Android Application - Day One
  2. Android Application – Day Two 
  3. How To Display A Dialog Window In Android 
Introduction
 
It is very common to display text like “Please wait” or loading.. in an Android application, when an application is performing a long-running task. If an application tries to connect to a server or do a calculation before displaying the result to the user, progress dialog gives the information about the status of the task.
 
Implementation 
 
First of all, create a project, as shown below:



Click Next as per instruction given in the wizard and you will finally reach the last step.


 
Coding

Add a button in activity_main.xml file, as shown below:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8. <Button  
  9. android:id="@+id/btnDialog"  
  10. android:layout_width="fill_parent"  
  11. android:layout_height="wrap_content"  
  12. android:text="click to display a dialog"  
  13. android:onClick="onClick"  
  14. />  
  15.   
  16. </LinearLayout>  

In MainActivity.java class, add the reference of ProgressDialog class

import android.app.ProgressDialog;

To create a progress dialog, you created an instance of the ProgressDialog class and called it’s show() method.

  1. public void onClick(View v)  
  2.  {  
  3.      //Show the dialog  
  4.      final ProgressDialog dialog= ProgressDialog.show(this,"Doing something""Please wait....",true);  
  5.      new Thread(new Runnable() {  
  6.          @Override  
  7.          public void run() {  
  8.              try {  
  9.              Thread.sleep(5000);  
  10.                  dialog.dismiss();  
  11.              }  
  12.              catch(InterruptedException ex){  
  13.                  ex.printStackTrace();  
  14.              }  
  15.          }  
  16.      }).start();  
  17.  }  
Run the app and get the result, depicted below:


In the screenshot, given above, it is a model dialog and will block the UI until, it is dismissed. We have created a Thread, using a Runnable block to perform a long running task. The code, given above, will execute in a separate thread and it is simulated to perform the task for five seconds by inserting a delay, using the sleep() method.

We can also create a more user friendly Progress Dialog and display the progress of an operation.



In the screenshot, given above, one more button is added in activity_main.xml

  1. <Button  
  2.      android:id="@+id/btnDialog2"  
  3.      android:layout_width="fill_parent"  
  4.      android:layout_height="wrap_content"  
  5.      android:text="Click to display a detailed progress dialog"  
  6.      android:onClick="onClick2"  
  7.      android:layout_below="@+id/btnDialog1"  
  8.      android:layout_alignParentLeft="true"  
  9.      android:layout_alignParentStart="true"  
  10.      android:layout_marginTop="200dp"  
  11.      />  

In the method, onCreateDialog(), add option for the second button will display a sophisticated Progress dialog. OK and Cancel buttons are set inside the progress dialog along with the various properties, such as icon, title and style.

Case 1:   

  1. progressDialog=new ProgressDialog(this);  
  2.   
  3.     progressDialog.setIcon(R.drawable.i1);  
  4.   
  5.     progressDialog.setTitle("Downloading files........");  
  6.   
  7.     progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);  
  8.   
  9.     progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",  
  10.   
  11.             new DialogInterface.OnClickListener() {  
  12.   
  13.                 @Override  
  14.   
  15.                 public void onClick(DialogInterface dialogInterface, int i) {  
  16.   
  17.                     Toast.makeText(getBaseContext(), "OK clicked", Toast.LENGTH_SHORT).show();  
  18.   
  19.                 }  
  20.   
  21.             }  
  22.   
  23.     );  
  24.   
  25.     progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",  
  26.   
  27.             new DialogInterface.OnClickListener() {  
  28.   
  29.                 @Override  
  30.   
  31.                 public void onClick(DialogInterface dialogInterface, int i) {  
  32.   
  33.                     Toast.makeText(getBaseContext(),"Cancel clicked", Toast.LENGTH_SHORT).show();  
  34.   
  35.                 }  
  36.   
  37.             });  
Add a method to display the progress status in the progress dialog by using a Thread object to run a Runnable block of the code.
  1. public void onClick2(View v) {  
  2.      showDialog(1);  
  3.      progressDialog.setProgress(0);  
  4.    
  5.      new Thread(new Runnable(){  
  6.          public void run(){  
  7.              for(int i=1;i<=10;i++){  
  8.                  try{  
  9.                      Thread.sleep(2000);  
  10.                      progressDialog.incrementProgressBy((int)(100/10));  
  11.                  }  
  12.                  catch (InterruptedException ex) {  
  13.                      ex.printStackTrace();  
  14.                  }  
  15.                  }  
  16.              progressDialog.dismiss();  
  17.              }  
  18.    
  19.      }).start();  
  20.  }  
Output
 


It is further incremented by the multiples of 10%.


Conclusion
 

The incrementProgressBy() method increments the counter in the progress dialog. When it reaches 100%, it is dismissed.Thus it is all about progress dialog and a detailed progress dialog control used in android app.

In the next article, I will explain about the Intents.


Similar Articles