Android tutorial - Android progress bar example - android app development - android studio - android development tutorial

Progress bar for android

Learn android - android tutorial - Progress bar for android - android examples - android programs

 dotted progress bar
  • Here, the android progress bar dialog box to display the status of work being done e.g. downloading file, analyzing status of work etc.
  • 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.
  • Progress bars are used to show progress of a task.
  • For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.
  • In android, there is a class called ProgressDialog that allows you to create progress bar.
  • In order to do this, you need to instantiate an object of this class. Its syntax is.
       ProgressDialog progress = new ProgressDialog(this);
click below button to copy the code from android tutorial team
  • A progress bar is a graphical control element used to visualize the progression of an extended computer operation, such as a
  • Sometimes, the graphic is accompanied by a textual representation of the progress in a percent format.

1. Add a Button

  • Open “res/layout/main.xml” file, just add normal button for Demo - android emulator - android tutorialnstration.
  • 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" />

click below button to copy the code from android tutorial team

2. Android 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.
  • 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;


click below button to copy the code from android tutorial team

3. Demo - android emulator - android tutorial

  • Run the application.
    • Result, a single button.
 processing state of application
  • Click on the button, it will prompt a "progress bar dialog" to show the current download progress.
 downloading progress
  • Task is completed, progress bar will show 100%, and close automatically.
 download completion

Related Searches to Progress bar android | Android progress bar example