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


Similar Articles