Android progress bar example

In Android, progress bar is useful to tell user that the task is takes longer time to finish.

In this tutorial, we show you how to display a progress bar dialog to tell user that your task is running, and also how to increase the progress bar status until the task is completed.

Refer to this Android ProgressBar JavaDoc for detail explanation.

P.S This project is developed in Eclipse 3.7, and tested with Android 2.3.3.

1. Add a Button

Open “res/layout/main.xml” file, just add normal button for demonstration.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
        android:text="Download File" />

2. Code Code

The key to use progress bar is using “Thread” to run your time consume task and another “Thread” to update the progress bar status accordingly. Read the code’s comment, it should be self-explanatory.

File :

import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAndroidAppActivity extends Activity {
	Button btnStartProgress;
	ProgressDialog progressBar;
	private int progressBarStatus = 0;
	private Handler progressBarHandler = new Handler();
	private long fileSize = 0;
	public void onCreate(Bundle savedInstanceState) {
	public void addListenerOnButton() {
		btnStartProgress = (Button) findViewById(;
                 new OnClickListener() {
		   public void onClick(View v) {
			// prepare for a progress bar dialog
			progressBar = new ProgressDialog(v.getContext());
			progressBar.setMessage("File downloading ...");
			//reset progress bar status
			progressBarStatus = 0;
			//reset filesize
			fileSize = 0;
			new Thread(new Runnable() {
			  public void run() {
				while (progressBarStatus < 100) {
				  // process some tasks
				  progressBarStatus = doSomeTasks();
				  // your computer is too fast, sleep 1 second
				  try {
				  } catch (InterruptedException e) {
				  // Update the progress bar Runnable() {
					public void run() {
				// ok, file is downloaded,
				if (progressBarStatus >= 100) {
					// sleep 2 seconds, so that you can see the 100%
					try {
					} catch (InterruptedException e) {
					// close the progress bar dialog
	// file download simulator... a really simple
	public int doSomeTasks() {
		while (fileSize <= 1000000) {
			if (fileSize == 100000) {
				return 10;
			} else if (fileSize == 200000) {
				return 20;
			} else if (fileSize == 300000) {
				return 30;
			// ...add your own
		return 100;

P.S The “doSomeTasks” method is just a file size download simulator, just replace this method with your long running task.

3. Demo

Run the application.

1. Result, a single button.

android progress bar demo1

2. Click on the button, it will prompt a “progress bar dialog” to show the current download progress.

android progress bar demo2

3. Task is completed, progress bar will show 100%, and close automatically.

android progress bar demo3

Download Source Code

Download it – (15 KB)


  1. Android ProgressBar Example
  2. Android ProgressDialog JavaDoc
  3. Another Android ProgressBar Example
Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: paypal loans()

  • Pingback: YouTube views kopen()

  • Pingback: laan penge nu og her()

  • Pingback: parking()

  • Pingback: penge laan nu()

  • Pingback: mobile porn()

  • Pingback: stop parking()

  • Pingback: lan penge nu sms()

  • Pingback:

  • Pingback: watch tv show episodes()

  • Pingback: Blue Coaster33()

  • hlman

    thanks for the tutorial..

  • hlman

    Very thanks to you..
    Nice tutorial

  • Kim

    This is extremely useful. One question: is there a way to make the progressBar “modal”. Right now, in my implementation of it, if the user taps outside the progressBar, it disappears and the main activity returns.

  • Sovankiry
  • Hyder

    Thanks. I want to get battery status in ProgressBar. I searched in web but no code is available.
    Thanks for tell how to use progress bar.

  • houssemzaier

    Simple nice and clear !
    Thanks Man

  • Pavan Kumar
  • Bharat

    Good that you watermark your images. Some guy has plagiarized your article on his blog without giving credit :

  • Ahsan
  • mayank

    sir,can we hide data from sd card. when we will put our sd card to other phone it show blank data.can it possible in android…

  • Kulbhushan

    in do some task while is necessory??

  • Pingback: ??????Android???????(ProgressBar) | ???()

  • Mahbubur R Aaman

    Excellent Tutorial. Want a tutorial on ProgressBar based on DialogFragment.

    • bbb

      no u cant…

  • monty

    Nice tutorial but it will work when i rotate the device.i am working on spinner progress dialog ,it works fine in any one mode (land or port) but after rotating the device ,it dismiss, so does it will work.

  • Plugie

    Why doesn’t the progress reset if I call the second time ?

  • Ganesh

    Thanks it is very usefull for me now….
    Thank once again.

  • Pingback: HTML und CSS App redirect -


    Thank you very much…it is very help me.
    But i want to play a local video with a upload button , help me

  • Stefano

    Hi, thank you very much for this example.

    Now: suppose I want to display a new activity after the file download is complete (e.g.. after progress bar dismissal): what code should I insert after “}).start();” and before the .startActivity of a new Intent ?


  • eduardo

    This is a progressdialog, not a progressbar! They are two different things!

  • Santosh

    Thanks helps a lot….

    • Vishnu Suresh

      Thanks for the helped me a lot.Keep posting more…

  • Pingback: How to Develop Android Apps | Kindle Android Apps and Covers()

  • arkiver

    If I have to insert a Toast which displays, “file downloaded successfully”. Which is a good place to insert that Toast in the above code ?

  • Viet Cuong

    Thank you very much :X

  • chathuranga

    thanks a lot!!! very useful. thanks again!

  • Salim

    Thank you very much…it help me