ProgressBar in Android

Introduction

This article explains how to use a Progress Bar.

Progress Bar

A Progress Bar is a graphical user interface that shows the progress of a task. Initially the Progress Bar does not show the progress in the form of a percentage. But now a Progress Bar shows progress in the form of a percentage. There are two types of Progress Bars.

ProgressDialogue Style Spinner

ProgressDialogue Style Horizontal

ProgressDialogue Style_Spinner

11-290x300.jpg

ProgressDialogue Style_Horizontal


1.jpg

In this you will use a button to start the progress bar in a click event. Inside the click event you will write this:

  1. progress = new ProgressDialog(v.getContext());  
  2.    
  3.   
  4. progress.setMessage("Show Progress ...");  
  5. progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  6. progress.setProgress(0);  
  7. progress.setMax(100);  
  8. progress.show();  
  9. progressStatus = 0;  
  10.   
  11. fileSize = 0;  
  12.   
  13.  new Thread(new Runnable()  
  14. {  
  15. public void run() {  
  16. while (progressStatus < 100) {  
  17.    
  18. progressStatus = doSomeTasks();  
  19.    
  20.  try {  
  21.     Thread.sleep(1000);  
  22.       } catch (InterruptedException e) {  
  23.         e.printStackTrace();  
  24.          }   
  25.          progressBarHandler.post(new Runnable() {  
  26.          public void run() {  
  27.              progress.setProgress(progressStatus);  
  28.                 }  
  29.                                    });  
  30.                             }                             
  31.  if (progressStatus >= 100) {  
  32.  try {  
  33.        Thread.sleep(2000);  
  34.       } catch (InterruptedException e)  
  35. {  
  36.        e.printStackTrace();  
  37.       }                                 
  38.       progress.dismiss();  
  39.     }  
  40.     }  
  41.  }).start();  

 

Step 1

Create an XML file and write this:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:layout_width="fill_parent"  
  4.               android:layout_height="fill_parent"  
  5.               android:orientation="vertical" >  
  6.    
  7.     <Button  
  8.             android:id="@+id/btnStartProgress"  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:text="StartProgress" />  
  12.    
  13. </LinearLayout>  

 

Step 2

Create a Java file and write this:

  1. package com.progress;  
  2.    
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.widget.Button;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10.    
  11. public class MainActivity extends Activity {  
  12.    
  13.     Button btnshowprogress;  
  14.     ProgressDialog progress;  
  15.     private int progressStatus = 0;  
  16.     private Handler progressBarHandler = new Handler();  
  17.    
  18.     private long fileSize = 0;  
  19.    
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.    
  25.    
  26.         btnshowprogress = (Button) findViewById(R.id.btnStartProgress);  
  27.         btnshowprogress.setOnClickListener(  
  28.                 new OnClickListener() {  
  29.    
  30.                     @Override  
  31.                     public void onClick(View v) {  
  32.    
  33.                         // prepare for a progress bar dialog  
  34.                         progress = new ProgressDialog(v.getContext());  
  35.    
  36.                         progress.setMessage("Show Progress ...");  
  37.                         progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  38.                         progress.setProgress(0);  
  39.                         progress.setMax(100);  
  40.                         progress.show();  
  41.    
  42.                         //reset progress bar status  
  43.                         progressStatus = 0;  
  44.    
  45.                         //reset filesize  
  46.                         fileSize = 0;  
  47.    
  48.                         new Thread(new Runnable() {  
  49.                             public void run() {  
  50.                                 while (progressStatus < 100) {  
  51.    
  52.                                     // process some tasks  
  53.                                     progressStatus = doSomeTasks();  
  54.    
  55.                                     // your computer is too fast, sleep 1 second  
  56.                                     try {  
  57.                                         Thread.sleep(1000);  
  58.                                     } catch (InterruptedException e) {  
  59.                                         e.printStackTrace();  
  60.                                     }  
  61.    
  62.                                     // Update the progress bar  
  63.                                     progressBarHandler.post(new Runnable() {  
  64.                                         public void run() {  
  65.                                             progress.setProgress(progressStatus);  
  66.                                         }  
  67.                                     });  
  68.                                 }  
  69.    
  70.                                 // ok, file is downloaded,  
  71.                                 if (progressStatus >= 100) {  
  72.    
  73.                                     // sleep 2 seconds, so that you can see the 100%  
  74.                                     try {  
  75.                                         Thread.sleep(2000);  
  76.                                     } catch (InterruptedException e) {  
  77.                                         e.printStackTrace();  
  78.                                     }  
  79.    
  80.                                     // close the progress bar dialog  
  81.                                     progress.dismiss();  
  82.                                 }  
  83.                             }  
  84.                         }).start();  
  85.    
  86.                     }  
  87.    
  88.                 });  
  89.    
  90.     }  
  91.    
  92.     // file download simulator... a really simple  
  93.     public int doSomeTasks() {  
  94.    
  95.         while (fileSize <= 1000000) {  
  96.    
  97.             fileSize++;  
  98.    
  99.             if (fileSize == 100000) {  
  100.                 return 10;  
  101.             } else if (fileSize == 200000) {  
  102.                 return 20;  
  103.             } else if (fileSize == 300000) {  
  104.                 return 30;  
  105.             }  
  106.             // ...add your own  
  107.    
  108.         }  
  109.    
  110.         return 100;  
  111.    
  112.     }  
  113.    
  114. }  

 

Step 3

Clipboard04.jpg

Step 4

When you will click on a button:

Clipboard10.jpg