Browse By

Android Progress Bar Example

When our application does a task that takes a considerable amount of time, it is good to show the progress of the task to the user. ProgressBar is a Visual indicator of progress in some operation. Displays a bar to the user representing how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward.

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.

In this tutorial we will create simple android application which will display progress when some one click on download button. We will display amount of % work has been completed in progress bar.

Direct Download Full Source Code



Android Progress Bar Example

Create New Android Project

  1. Create a new project and fill the required details File ⇒ New ⇒ Android Project
  2. Open  main.xml (res/layout) and simply put Download button for launching Progress dialog
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
    	android:orientation="vertical" android:layout_width="fill_parent"
    	<TextView android:textColor="#fff" android:textSize="20dp"
    		android:layout_height="wrap_content" android:id="@+id/textView1"
    		android:text="Pulse 7 Demo" android:layout_width="wrap_content"
    	<TextView android:textColor="#fff" android:textSize="15dp"
    		android:layout_height="wrap_content" android:id="@+id/textView2"
    		android:text="Click Download Button to Launch Progress Bar "
    		android:layout_width="wrap_content" android:layout_gravity="center_horizontal"
    	<Button android:layout_height="wrap_content" android:id="@+id/btnDownload" android:text="Download File" android:layout_width="match_parent"></Button>
  3. Now open your Activity Class and create object of ProgressDialog to display progress dialog. You need to use Handler class to update progress status.  Put following code in your activity class.
    package com.vrs.progressbar;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    public class Pulse7ProgressBarActivity extends Activity implements
    		OnClickListener {
    	// Widget GUI
    	Button btnDownload;
    	ProgressDialog progressBar;
    	// Handler for updating progress
    	Handler progressBarHandler;
    	// Activity Variable
    	int progressStatus;
    	int fileSize;
    	/** Called when the activity is first created. */
    	public void onCreate(Bundle savedInstanceState) {
    		// Init Widget and attached click listener
    		btnDownload = (Button) findViewById(;
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		// Init Progress Dialog
    		progressBarHandler = new Handler();
    		progressBar = new ProgressDialog(this);
    		progressBar.setMessage("Downloading File...");
    		progressStatus = 0;
    		// Start Thread to execute progress dialog
    		new Thread(new Runnable() {
    			public void run() {
    				while (progressStatus < 100) {
    					// do file downnloading task and update progress status
    					progressStatus = downloadFile();
    					try {
    						// pause to see progress
    					} catch (Exception e) {
    					// Use Handler to update progress in progress dialog Runnable() {
    						public void run() {
    					// Dismiss Progress is progress is complete
    					if (progressStatus >= 100) {
    	// Simulator for donwloading file...
    	private int downloadFile() {
    		while (fileSize <= 1000000) {
    			if (fileSize == 100000) {
    				return 10;
    			} else if (fileSize == 200000) {
    				return 20;
    			} else if (fileSize == 300000) {
    				return 30;
    			} else if (fileSize == 400000) {
    				return 40;
    			} else if (fileSize == 500000) {
    				return 50;
    			} else if (fileSize == 600000) {
    				return 60;
    			} else if (fileSize == 700000) {
    				return 70;
    			} else if (fileSize == 800000) {
    				return 80;
    			} else if (fileSize == 900000) {
    				return 90;
    		return 100;
  4. Now execute application

    Android Progress Bar Example

    When you click on Download button it will display progress dialog box.

Points to Remember

  1. You need to create new background thread for perform task like downloading file.
  2. You need Handler class to update progress of task, in progress dialog

I hope you like this article. Share your views to to improve content.

Download Full Source Code