Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

ProgressBar


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.     <Button
  7.         android:id=“@+id/button1”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentTop=“true”
  11.         android:layout_centerHorizontal=“true”
  12.         android:layout_marginTop=“116dp”
  13.         android:text=“download file” />
  14. </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.            @Override
  26.            public void onClick(View v) {
  27.             // creating progress bar dialog
  28.             progressBar = new ProgressDialog(v.getContext());
  29.             progressBar.setCancelable(true);
  30.             progressBar.setMessage(“File downloading …”);
  31.             progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
  32.             progressBar.setProgress(0);
  33.             progressBar.setMax(100);
  34.             progressBar.show();
  35.             //reset progress bar and filesize status
  36.             progressBarStatus = 0;
  37.             fileSize = 0;
  38.             new Thread(new Runnable() {
  39.               public void run() {
  40.                 while (progressBarStatus < 100) {
  41.                   // performing operation
  42.                   progressBarStatus = doOperation();
  43.                   try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
  44.                   // Updating the progress bar
  45.                   progressBarHandler.post(new Runnable() {
  46.                     public void run() {
  47.                       progressBar.setProgress(progressBarStatus);
  48.                     }
  49.                   });
  50.                 }
  51.                 // performing operation if file is downloaded,
  52.                 if (progressBarStatus >= 100) {
  53.                     // sleeping for 1 second after operation completed
  54.                     try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}
  55.                     // close the progress bar dialog
  56.                     progressBar.dismiss();
  57.                 }
  58.               }
  59.              }).start();
  60.             }//end of onClick method
  61.           });
  62.          }
  63.     // checking how much file is downloaded and updating the filesize 
  64.     public int doOperation() {
  65.         //The range of ProgressDialog starts from 0 to 10000
  66.         while (fileSize <= 10000) {
  67.             fileSize++;
  68.             if (fileSize == 1000) {
  69.                 return 10;
  70.             } else if (fileSize == 2000) {
  71.                 return 20;
  72.             } else if (fileSize == 3000) {
  73.                 return 30;
  74.             } else if (fileSize == 4000) {
  75.             return 40;//you can add more else if
  76.             } else{
  77.                 return 100;
  78.             }
  79.         }//end of while
  80.         return 100;
  81.     }//end of doOperation
  82.     @Override
  83.     public boolean onCreateOptionsMenu(Menu menu) {
  84.         // Inflate the menu; this adds items to the action bar if it is present.
  85.         getMenuInflater().inflate(R.menu.main, menu);
  86.         return true;
  87.     }
  88. }

Output:

progressoutput1

progressoutput2

Comments

comments