Android ProgessBar Example


Hi, in this tutorial you will learn how to use a ProgressBar element in your Android application. ProgessBar is a visual element that show a progress that has been made in some operation. It is known as good practice to use this element if you have any long lasting operations in your application, this way you can notify your user of how far the operation has progressed. 


1. Application Layout acivity_main.xml file

           As usual we will start with the creation of the application layout. We will be adding three elements to it: ProgressBar and two Button elements. To do that go to the res\layout directory and type the following code into the activity_main.xml file:
 <LinearLayout 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"  
   android:orientation="vertical"  
   android:background="#696969"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity" >  
   <ProgressBar   
     android:id="@+id/oure_progress_bar"  
     style="?android:attr/progressBarStyleHorizontal"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:paddingTop="100sp"/>  
   <TableRow  
     android:id="@+id/linear"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:padding="20sp">  
     <Button   
       android:id="@+id/start"  
       android:layout_height="wrap_content"  
       android:layout_width="match_parent"  
       android:text="@string/start_text"  
       android:layout_weight="1"/>  
     <Button   
       android:id="@+id/stop"  
       android:layout_height="wrap_content"  
       android:layout_width="match_parent"  
       android:text="@string/stop_text"  
       android:layout_weight="1"/>  
     </TableRow>  
 </LinearLayout>  

2. Creating a New Thread 

            To perform a long lasting task it is better to use a separate thread. Android provides Handler class that will help us start a new thread in the background and interact with the user interface. After we create an instance of the Handler class, our thread will be able to communicate with the Handler object, which will update the user interface in the main activity thread.
            Go to the MainActivity.java class and type the following code into it: 
 package com.example.progressbarexample;  
 import android.os.Bundle;  
 import android.os.Handler;  
 import android.os.Message;  
 import android.app.Activity;  
 import android.util.Log;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.ProgressBar;  
 public class MainActivity extends Activity {  
      private ProgressBar progressBar;  
      private int status = 0;  
      private Button startButton;  
      private Button stopButton;  
      private boolean running = false;  
      private Handler our_handler;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           progressBar = (ProgressBar) findViewById(R.id.oure_progress_bar);  
           startButton = (Button) findViewById(R.id.start);  
           stopButton = (Button) findViewById(R.id.stop);  
           our_handler = new Handler(){  
           public void handleMessage(Message msg){  
                progressBar.incrementProgressBy(1);  
                }};  
                startButton.setOnClickListener(new OnClickListener() {  
                     @Override  
                     public void onClick(View v) {  
                          onStart();  
                     }  
                });  
                stopButton.setOnClickListener(new OnClickListener() {                      
                     @Override  
                     public void onClick(View v) {  
                          onStop();                           
                     }  
                });  
      }       
      public void onStart(){  
           super.onStart();  
           progressBar.setProgress(status);  
           Thread background = new Thread(new Runnable() {  
                @Override  
                public void run() {  
                     while(running)  
                          try{  
                               Thread.sleep(100);  
                               our_handler.sendMessage(our_handler.obtainMessage());  
                          }  
                          catch (InterruptedException e) {  
                               Log.e("Error", "Thread Interrupted");  
                          }  
                }  
           });            
           running = true;  
           background.start();  
      }  
      public void onStop(){  
           super.onStop();  
           running = false;  
      }  
 }  

DEMO

Source Code