AdSense

AdSense3

Friday, 7 August 2015

Android ProgressBar Example

android progress dialog
We can display the android progress bar dialog box to display the status of work being done e.g. downloading file, analyzing status of work etc.
In this example, we are displaying the progress dialog for dummy file download operation.
Here we are using android.app.ProgressDialog class to show the progress bar. Android ProgressDialog is the subclass of AlertDialog class.
The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), setProgressStyle(), setMax(), show() etc. The progress range of Progress Dialog is 0 to 10000.
Let's see a simple example to display progress bar in android.
  1. ProgressDialog progressBar = new ProgressDialog(this);  
  2. progressBar.setCancelable(true);//you can cancel it by pressing back button  
  3. progressBar.setMessage("File downloading ...");  
  4. progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  5. progressBar.setProgress(0);//initially progress is 0  
  6. progressBar.setMax(100);//sets the maximum value 100  
  7. progressBar.show();//displays the progress bar  

Android Progress Bar Example by ProgressDialog

Let's see a simple example to create progress bar using ProgressDialog class.

activity_main.xml

Drag one button from the pallete, now the activity_main.xml file will look like this:
File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="116dp"  
  14.         android:text="download file" />  
  15.   
  16. </RelativeLayout>  

Activity class

Let's write the code to display the progress bar dialog box.
File: MainActivity.java
  1. package com.example.progressbar1;  
  2. import android.app.Activity;  
  3. import android.app.ProgressDialog;  
  4. import android.os.Bundle;  
  5. import android.os.Handler;  
  6. import android.widget.Button;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. public class MainActivity extends Activity {  
  11.     Button btnStartProgress;  
  12.     ProgressDialog progressBar;  
  13.     private int progressBarStatus = 0;  
  14.     private Handler progressBarHandler = new Handler();  
  15.         private long fileSize = 0;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         addListenerOnButtonClick();  
  21.     }  
  22.     public void addListenerOnButtonClick() {  
  23.         btnStartProgress = (Button) findViewById(R.id.button1);  
  24.         btnStartProgress.setOnClickListener(new OnClickListener(){  
  25.    
  26.            @Override  
  27.            public void onClick(View v) {  
  28.             // creating progress bar dialog  
  29.             progressBar = new ProgressDialog(v.getContext());  
  30.             progressBar.setCancelable(true);  
  31.             progressBar.setMessage("File downloading ...");  
  32.             progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  33.             progressBar.setProgress(0);  
  34.             progressBar.setMax(100);  
  35.             progressBar.show();  
  36.             //reset progress bar and filesize status  
  37.             progressBarStatus = 0;  
  38.             fileSize = 0;  
  39.    
  40.             new Thread(new Runnable() {  
  41.               public void run() {  
  42.                 while (progressBarStatus < 100) {  
  43.                   // performing operation  
  44.                   progressBarStatus = doOperation();  
  45.                   try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  46.                   // Updating the progress bar  
  47.                   progressBarHandler.post(new Runnable() {  
  48.                     public void run() {  
  49.                       progressBar.setProgress(progressBarStatus);  
  50.                     }  
  51.                   });  
  52.                 }  
  53.                 // performing operation if file is downloaded,  
  54.                 if (progressBarStatus >= 100) {  
  55.                     // sleeping for 1 second after operation completed  
  56.                     try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}  
  57.                     // close the progress bar dialog  
  58.                     progressBar.dismiss();  
  59.                 }  
  60.               }  
  61.              }).start();  
  62.             }//end of onClick method  
  63.           });  
  64.          }  
  65.     // checking how much file is downloaded and updating the filesize   
  66.     public int doOperation() {  
  67.         //The range of ProgressDialog starts from 0 to 10000  
  68.         while (fileSize <= 10000) {  
  69.             fileSize++;  
  70.             if (fileSize == 1000) {  
  71.                 return 10;  
  72.             } else if (fileSize == 2000) {  
  73.                 return 20;  
  74.             } else if (fileSize == 3000) {  
  75.                 return 30;  
  76.             } else if (fileSize == 4000) {  
  77.             return 40;//you can add more else if  
  78.             } else{  
  79.                 return 100;  
  80.             }  
  81.         }//end of while  
  82.         return 100;  
  83.     }//end of doOperation  
  84.   
  85.     @Override  
  86.     public boolean onCreateOptionsMenu(Menu menu) {  
  87.         // Inflate the menu; this adds items to the action bar if it is present.  
  88.         getMenuInflater().inflate(R.menu.main, menu);  
  89.         return true;  
  90.     }  
  91. }  

Output:

android progress bar example output 1 android progress bar example output 2

1 comment:

  1. i was looking simple and easy progressbar tutorial.
    Finally i found it
    Kundan, your blogs are very helpful for beginners

    ReplyDelete