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. <Button
  8. android:id="@+id/btnDialog"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="click to display a dialog"
  12. android:onClick="onClick"
  13. />
  14. </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. progressDialog.setIcon(R.drawable.i1);
  3. progressDialog.setTitle("Downloading files........");
  4. progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
  5. progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
  6. new DialogInterface.OnClickListener() {
  7. @Override
  8. public void onClick(DialogInterface dialogInterface, int i) {
  9. Toast.makeText(getBaseContext(), "OK clicked", Toast.LENGTH_SHORT).show();
  10. }
  11. }
  12. );
  13. progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
  14. new DialogInterface.OnClickListener() {
  15. @Override
  16. public void onClick(DialogInterface dialogInterface, int i) {
  17. Toast.makeText(getBaseContext(),"Cancel clicked", Toast.LENGTH_SHORT).show();
  18. }
  19. });
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. new Thread(new Runnable(){
  5. public void run(){
  6. for(int i=1;i<=10;i++){
  7. try{
  8. Thread.sleep(2000);
  9. progressDialog.incrementProgressBy((int)(100/10));
  10. }
  11. catch (InterruptedException ex) {
  12. ex.printStackTrace();
  13. }
  14. }
  15. progressDialog.dismiss();
  16. }
  17. }).start();
  18. }
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.