Android Progress Bar Example


          This tutorial is about Android ProgressBar. If you are looking for ProgressDialog then go to this page Android ProgressDialog example.

What is Android ProgressBar ?
          ProgressBar in Android is a graphical view indicator which shows some progress. It shows a bar representing the completing of the task. Normally the ProgresBar do not display the amount of completion in numbers. If we want, we can display it in the TextView (Given in the example).


Difference between ProgressBar and ProgressDialog:
           The programmers often get this doubt. ProgressBar is a view indicating the amount of progress done. The application can change the value. This can be used in the layout to show the progress.
On the other side ProgressDialog is a dialog box (like alert). The user must wait for the progress to complete.

Important things about ProgressBar:
            By default the progress bar maximum value is 100. It can also be changed by setting the our own maximum value using android:max attribute. Example;



If we know the amount of work, then we can use this progress bar showing the actual progress. But if we do not know the amount of work to be done, then we can use progressBar.setIndeterminate(true) in the activity class programatically or using android:indeterminate="true" in the layout xml file which will enable the intermediate mode. In the intermediate mode the actual progress will not be shown. A cyclic animation will be shown to indicate that some progress is happening. This intermediate mode is shown below;

By default the progress bar will be displayed as a spinning wheel. If we want it to be displayed as a horizontal bar, we need to use  style="?android:attr/progressBarStyleHorizontal" attribute.
The default mode is shown below;

How to create:
            ProgressBar can be created using <ProgressBar> tag in the layout xml file. android:max attribute is used to set the maximum value of the progress bar. android:minHeight attribute is used to set the height of the progress bar. androif:minWidth attribute is used to set the width of the progress bar. android:progress attribute is used to set the number in which the progress bar value will be incremented.

Example program:
            In the example program I am creating the ProgressBar and updating the value of the progress bar inside a thread. I make the the thread to sleep for 200 milliseconds after incrementing the value to show the progress slowly.

Android layout file: activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="20dp"
        android:indeterminate="false"
        android:max="100"
        android:minHeight="50dp"
        android:minWidth="200dp"
        android:progress="1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/progressBar1"
        android:layout_below="@+id/progressBar1"/>

</RelativeLayout>


Activity class: MainActivity.java
package com.example.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Android ProgressBar example.
 * @author Prabu Dhanapal
 * @version 1.0
 * @since Sep 01 2013
 *
 */
public class MainActivity extends Activity {
 private ProgressBar progressBar;
 private int progressStatus = 0;
 private TextView textView;
 private Handler handler = new Handler();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  progressBar = (ProgressBar) findViewById(R.id.progressBar1);
  textView = (TextView) findViewById(R.id.textView1);
  // Start long running operation in a background thread
  new Thread(new Runnable() {
     public void run() {
        while (progressStatus < 100) {
           progressStatus += 1;
    // Update the progress bar and display the 
                         //current value in the text view
    handler.post(new Runnable() {
    public void run() {
       progressBar.setProgress(progressStatus);
       textView.setText(progressStatus+"/"+progressBar.getMax());
    }
        });
        try {
           // Sleep for 200 milliseconds. 
                         //Just to display the progress slowly
           Thread.sleep(200);
        } catch (InterruptedException e) {
           e.printStackTrace();
        }
     }
  }
  }).start();
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items 
                //to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
}


Output screenshots:





Source code of this application:
ProgressBar.zip

 



Reactions:

4 comments :

  1. Hello thanks for your code, I just need to know how can i implemment a stop button, i want to press the button and progress bar stops. Can you help me ?

    Thanks

    ReplyDelete
    Replies
    1. Hi,

      Did you realize the stop button ?

      Delete
  2. hi, please help me out.....
    am simply trying to show a dialog while I am grabbing some data off a URL and then dismiss properly after the data is retrieved.
    How can I go about it?

    ReplyDelete
  3. Hi, I do not know the amount of work to be done and i want to use that progress bar which you have shown in this post. I tried to use progress bar by setting the drawable but every time i failed to achieve that progress bar. Can you please give me the code and images for ” indeterminate progress bar” which google use when downloading from playstore.
    Thanks.

    ReplyDelete