Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Monday, 7 July 2014

Android ProgressBar Example

We can display the progress bar dialog box to display the status of work being done e.g. downloading file etc.
In this example, we are displaying the progress dialog for dummy file download.
The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(), show() etc.


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.   
  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.Menu;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11.   
  12. public class MainActivity extends Activity {  
  13.     Button btnStartProgress;  
  14.     ProgressDialog progressBar;  
  15.     private int progressBarStatus = 0;  
  16.     private Handler progressBarHandler = new Handler();  
  17.     private long fileSize = 0;  
  18.       
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.         addListenerOnButtonClick();  
  24.     }  
  25.       
  26.     public void addListenerOnButtonClick() {  
  27.            
  28.         btnStartProgress = (Button) findViewById(R.id.button1);  
  29.         btnStartProgress.setOnClickListener(new OnClickListener(){  
  30.    
  31.            @Override  
  32.            public void onClick(View v) {  
  33.    
  34.             // creating progress bar dialog  
  35.             progressBar = new ProgressDialog(v.getContext());  
  36.             progressBar.setCancelable(true);  
  37.             progressBar.setMessage("File downloading ...");  
  38.             progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);  
  39.             progressBar.setProgress(0);  
  40.             progressBar.setMax(100);  
  41.             progressBar.show();  
  42.    
  43.             //reset progress bar and filesize status  
  44.             progressBarStatus = 0;  
  45.             fileSize = 0;  
  46.    
  47.             new Thread(new Runnable() {  
  48.               public void run() {  
  49.                 while (progressBarStatus < 100) {  
  50.    
  51.                   // performing operation  
  52.                   progressBarStatus = doOperation();  
  53.    
  54.                   try {  
  55.                     Thread.sleep(1000);  
  56.                   } catch (InterruptedException e) {  
  57.                     e.printStackTrace();  
  58.                   }  
  59.    
  60.                   // Updating the progress bar  
  61.                   progressBarHandler.post(new Runnable() {  
  62.                     public void run() {  
  63.                       progressBar.setProgress(progressBarStatus);  
  64.                     }  
  65.                   });  
  66.                 }  
  67.    
  68.                 // performing operation if file is downloaded,  
  69.                 if (progressBarStatus >= 100) {  
  70.    
  71.                     // sleeping for 1 second after operation completed  
  72.                     try {  
  73.                         Thread.sleep(1000);  
  74.                     } catch (InterruptedException e) {e.printStackTrace();}  
  75.    
  76.                     // close the progress bar dialog  
  77.                     progressBar.dismiss();  
  78.                 }  
  79.               }  
  80.              }).start();  
  81.             }//end of onClick method  
  82.           });  
  83.          }  
  84.    
  85.     // checking how much file is downloaded and updating the filesize   
  86.     public int doOperation() {  
  87.         //The range of ProgressDialog starts from 0 to 10000  
  88.         while (fileSize <= 10000) {  
  89.             fileSize++;  
  90.             if (fileSize == 1000) {  
  91.                 return 10;  
  92.             } else if (fileSize == 2000) {  
  93.                 return 20;  
  94.             } else if (fileSize == 3000) {  
  95.                 return 30;  
  96.             } else if (fileSize == 4000) {  
  97.             return 40;  
  98.             } else if (fileSize == 5000) {  
  99.                 return 50;  
  100.             } else if (fileSize == 6000) {  
  101.                 return 60;  
  102.             }  
  103.              else if (fileSize == 7000) {  
  104.                     return 70;  
  105.             }  
  106.              else if (fileSize == 8000) {  
  107.                     return 80;  
  108.             }  
  109.              else if (fileSize == 9000) {  
  110.                     return 90;  
  111.             }  
  112.              else if (fileSize == 10000) {  
  113.                     return 100;  
  114.             }  
  115.         }//end of while  
  116.         return 100;  
  117.     }//end of doOperation  
  118.   
  119.     @Override  
  120.     public boolean onCreateOptionsMenu(Menu menu) {  
  121.         // Inflate the menu; this adds items to the action bar if it is present.  
  122.         getMenuInflater().inflate(R.menu.main, menu);  
  123.         return true;  
  124.     }  

0 comments:

Post a Comment

Followers