Android ProgressDialog (Spinner Style) Example

Flexus Business Services

Android Progress Dialog (Spinner Style) –

ProgressDialog is a child class of AlertDialog.It is used to display the progress of any task.and it automatically disappears when task is completed.there are two types of Progress Dialog one is Horizontal Style and another is Spinner style.

Contants for Styling the ProgressDialog –

public static final int STYLE_HORIZONTAL

Creates a ProgressDialog with a horizontal progress bar.

public static final int STYLE_SPINNER

Creates a ProgressDialog with a circular, spinning progress bar. This is the default.

So in this tutorial I am discussing the Spinner Style Progress Dialog.

Code Sample-

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/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="beginYourTask"
        android:text="Click here to start your task" />

</LinearLayout>

AndroidProgressDialogCyclicActivity.java

package com.ui.yogeshblogspot;

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

public class AndroidProgressDialogCyclicActivity extends Activity {
    /** Called when the activity is first created. */
	private static int myProgress;
	  private ProgressDialog progressDialog;
	  private Handler myHandler=new Handler();
	   private int progressStatus=0;
	   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    public void beginYourTask(View v)
    {
    progressDialog=new ProgressDialog(v.getContext());
    progressDialog.setCancelable(true);
    progressDialog.setMessage("Initializing Please Wait");
    progressDialog.setTitle("Loading");
    progressDialog.setIcon(R.drawable.home_add);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setProgress(0);
    progressDialog.setMax(100);
    progressDialog.show();
    progressStatus=0;
    new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(progressStatus<10)
				{
					progressStatus=performTask();
					
					
				}
				/*Dismiss the Progress bar*/
				myHandler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						progressDialog.dismiss();//dismiss the dialog
					   Toast.makeText(getBaseContext(),"Task Completed",Toast.LENGTH_LONG).show();
	                   progressStatus=0;
	                   
	                   myProgress=0;
						
					}
				});
				
			}
			/* Do some task*/
			private int performTask()
			{
				try {
					//---simulate doing some work---
					Thread.sleep(1000);
					} catch (InterruptedException e)
					{
					e.printStackTrace();
					}
					return ++myProgress;	
			}
		}).start();
}	
}
   

Now let’s discuss the code sample step by step –

So first I will discuss the main.xml file. In the main.xml file I have created a button view component only.

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="beginYourTask"
        android:text="Click here to start your task" />

And when user click on button I am calling beginYourTask method.which is defined on Activity Class.

Now lets discuss the activity class step by step –

So now let’s discuss the Activity class.in this file I am creating some private fields to store and handle progress status and progress.

     private static int myProgress=0; <!- - to store the total progress - ->
private ProgressDialog progressDialog; <!- -to store the reference of ProgressDialog -- > 
private Handler myHandler=new Handler();<!- -To handle to Progress Dialog -- > 
private int progressStatus=0; <!- - To Store the Current Progress Status --  >

now let’s discuss the beginYourTask() method.

progressDialog=new ProgressDialog(v.getContext());

By this code I am creating a object of ProgressDialog and then assigining the reference of this dialog to progressDialog reference variable

progressDialog.setCancelable(true);

In the setCancelable method of ProgressDialog I am passing true so that user can cancel this dialog box by pressing back button.

progressDialog.setMessage(“Initializing Please Wait”);

setMessage() method is used to display the message in the ProgressDialog.

progressDialog.setTitle(“Loading”);

setTitle() method is used to set The title of ProgressDialog.

progressDialog.setIcon(R.drawable.home_add);
setIcon() method is used to display an icon in the title of ProgressDialog.

progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

By using this method I am styling the progress dialog as Spinner style.

progressDialog.setProgress(0);

By calling this method I am setting the initial progress to 0.

progressDialog.setMax(100);

By calling this method I am setting max progress of this ProgressDialog to 100.

progressDialog.show();
Progress dialog is become visible when we call show() method.

Then I am creating a new thread to perform background task.and inside the run() method of this thread I am calling performTask() multiple times until task is completed.And code sample is –

                       
                            while(progressStatus<10)
				{
					progressStatus=performTask();
					
			         }
				

And when the task is completed I am again calling post method to handle this state.

                /*Dismiss the Progress bar*/
				myHandler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						progressDialog.dismiss();//dismiss the dialog
					   Toast.makeText(getBaseContext(),"Task Completed",Toast.LENGTH_LONG).show();
	                   progressStatus=0;
	                   
	                   myProgress=0;
						
					}
				});

In this method I am destroying the dialog box by calling the dismiss() method.and I am also giving a message that “Task Completed” and then assigning progresStatus and myProgress value to “0”(initial value).
Reason behind assigning initial value is that when user again clicks on button same task will be start again from initial state.


Output-

Progress Dialog






















Progress Dialog






















Progress Dialog






















Click here to Download AndroidProgressDialogCyclic.zip

1 Comment

Leave a Reply