Android progress bar

In this Android tip, I am going to show you how to use the ProgressBar widget to display a progress bar to the user. A progress bar tells the user how far an operation progressed. To define a progress bar in a layout xml file, you will use the <ProgressBar> tag. There are two styles of the progress bar: spinning wheel, and horizontal bar. By default, the <ProgressBar> tag shows a spinning wheel. The spinning wheel style is used when the length of an operation is unknown. If you want the second style you can use the style attribute as shown below.
The application that uses the progress bar can specify its starting and max values. While the operation is progressing, you can use a worker thread to update the current value of the progress bar.
Now to have an example application on using ProgessBar, you create a new Android project. Then modify the activity_main.xml file to add a ProgessBar as shown below.

<LinearLayout xmlns:android=""

In the MainActivity class, you will create a reference to progress bar, set its starting and max values, and setup a worker thread to start and update the progress bar as shown below.

package com.example.androidexample;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
  Handler handler;
  ProgressBar pb;
  int maxValue=100;
  int curValue=0;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main); pb=(ProgressBar)findViewById(;
//set maximum value to progressbar
//set the start value to progressbar
//create an Instance of Handler
handler=new Handler();
//start Runnable;
Runnable UpdateProgess=new Runnable(){
  public void run(){
  //delay 1/2s before next call
  handler.postDelayed(this, 500);  
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;


Posted by: Dara | post date: 09-15-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.