Want to run a Progress dialog in your Android apps? Here is the way...

            Hey loving readers,in this post I will discuss about the Progress Dialog features in a android application.Usually we use it,when our application needs some time to process a request or waiting for a resource like internet connection or anything.So here I build a demo application to show you, how the progress dialog actually works.
            In this application we take a button.When we press the button,the progress dialog will appear and it also give the users information that what the application is doing in the background.Int this application we just count 0-900 value in background.You can show the counting in the logcat view(Eclipse IDE is used here).The progress dialog also give some information,when and what the application is counting.

screenshot 1
screenshot 2
screenshot 3

     After the counting process is over,the progress dialog will be dismissed and a toast will be  shown to inform the user that the counting process is completed.Then  the button can be pressed again and the counting process will begin.So simple! Isn't it?
     So lets take a look at the xml layout.It is really simple one with a button only with the name "Counting Button". Lets take a look....

<?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/button1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Counting Button" 
        android:layout_gravity="center" 
        android:layout_marginTop="100dp"
        />
</LinearLayout>

           Now it;s time for some java code,that actually runs the progress dialog.Note here, that we don't define a Progress Dialog widget in the xml file.This is not provided by the ADT plugin(Eclipse platform) as we don't need a Progress dialog at start up.We need it,when an event occurs,that requires some time.So we define it in the java file.Lets take a look....

package com.androidcookers.progressdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
/**
 * @author www.androidcookers.co.cc
 * {@link www.androidcookers.co.cc}
 *
 */
public class MyProgressbarActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button btn=(Button)findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {    
    new Progress().execute("ASD");
   }
  });
    }
    private class Progress extends AsyncTask{
     ProgressDialog pd;
     int count=0;
  public void onPreExecute(){
   pd=new ProgressDialog(MyProgressbarActivity.this);   
   pd.show();
  }
  @Override
  protected String doInBackground(String... params) {
   publishProgress(new Integer(1));
   for(int i=0;i<=300;i++){
    System.out.println(i);
   }
   publishProgress(new Integer(1));
   for(int i=301;i<=600;i++){
    System.out.println(i);
   }
   publishProgress(new Integer(1));
   for(int i=601;i<=900;i++){
    System.out.println(i);
   }
   pd.dismiss();//dismiss the progress dialog here...
   return null;
  }
  protected void onProgressUpdate(Integer... values){
   count++;
   if(count==1){
    pd.setMessage("Now counting 0 to 300...");  
   }
   if(count==2){
    pd.setMessage("Now counting 301 to 600...");
   }
   if(count==3){
    pd.setMessage("Now counting 601 to 900...");
   }
  }
  protected void onPostExecute(String result) {
   Toast.makeText(MyProgressbarActivity.this,"Counting completed!", Toast.LENGTH_LONG).show();
  }
    }     
}

             In this java file,first we initialize the button and when the button is pressed the Progress class is called.This class extends AsyncTask.That enable us to run a thread in the background,that process the counting task.It make us to implement the doInBackground() method.In this method the actual counting process is defined.We additionally define three other methods named onPreExecute(),onProgressUpdate() and the onPostExecute().
             The first one[onPreExecute()] makes a progress dialog object and initialize it. onProgressUpdate() method give us information when and what is counting. onPostExecute() method defines what will happen when the Progress dialog will be dismissed.In this application we showed a toast here.
             Don't forget to dismiss the progress dialog at the end of doInBackground() method by calling the dismiss method.Otherwise the progress dialog will continuously rotate even the background counting process is over.

No comments:

Post a Comment