CS453 | mobile programming
  • outline
  • projects
  • syllabus
  • links

ProgressBar View

--see (Books/Android/Source Code/BasicViews2)

Progress Bar is used to give user feedback about some ongoing task (possibly in background)

  • the "work" of the program that we want to monitor its progress will be coded for here as a separate Thread
  • START Progress Bar: the progress bar shows by default a spinning wheel --- it will start automatically -- in this case progress is just ongoing and stopped (see next)
  • UPDATE Progess Bar:
    • in its default style, there is no need to update, it is on/off case
    • you can alter the style so you can update the progress, in xml add parameter
      style="@android:style/Widget.ProgressBar.Horizontal"       //in ProgressBar tag of XML file
      
      
      ***********************************************
      
      //Now in your code when you have done some work and want to update do the following
      progressBar.setProgress(progressStatus);
      	              
  • STOP Progress Bar:
    • you must basically hide the progress bar
      progressBar.setVisibility(android.view.View.GONE)
  • EXAMPLE
    • In the code below we try to do some work 10 times (see while( progressStatus< 10) )
    • When you have completed doing the work 10 times, use an android.os.Handler instance to "STOP "

main.xml ( activities defined GUI in xml)progress bar about 1/3 through the 10 job itereations we will do

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<ProgressBar android:id="@+id/progressbar"
 	        android:layout_width="wrap_content" 
 	        android:layout_height="wrap_content"
 	        style="@android:style/Widget.ProgressBar.Horizontal" />   sets up as Horizontal style
</LinearLayout>

BasicViews2Activity.java

package net.learn2develop.BasicViews2;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
public class BasicViews2Activity extends Activity {
 	   static int progress;
 	   ProgressBar progressBar;
 	   int progressStatus = 0;   //represents progress status, initialize to 0
 	   Handler handler = new Handler();  //handler for
 /** Called when the activity is first created. */
 	   @Override
 	   public void onCreate(Bundle savedInstanceState) {
 	        super.onCreate(savedInstanceState);  //START THE PROGRESS BAR
 	        setContentView(R.layout.main);
   
 	        progress = 0;
 	        progressBar = (ProgressBar) findViewById(R.id.progressbar);  //grab handle to GUI ProgressBar in xml file
 	        progressBar.setMax(200);
   
 	        //---do some work in background thread-----this is where we will do work and update progress bar
             //   do the work as a separate Thread whe will with start() method invocation below start running
new Thread(new Runnable() //IN YELLOW showing the anonymous instance of Runnable class createing here { public void run() {      //—-do some work here—-     while (progressStatus < 100)     {         progressStatus = doSomeWork();
                     //—-Update the progress bar—-
         	        handler.post(new Runnable()
            	            {
 	                       public void run() {
 	                            progressBar.setProgress(progressStatus); //update ProgressBar
 	                        }
 	                 });
 	            }
                 //---hides the progress bar---
     	        handler.post(new Runnable()
 	                {
 	                    public void run()
 	                    {
 	                        //---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
 	                        progressBar.setVisibility(View.GONE);  //stop ProgressBar by hiding it
 	                    }
 	             });
 	         }
 
              //---do some long running work here---
 	         private int doSomeWork() 
 	         {
 	            try {
 	                //---simulate doing some work---
 	                Thread.sleep(50);
 	            } catch (InterruptedException e)
 	            {
 	                e.printStackTrace();
 	            }
 	   
                 return ++progress;
 	         }
         }).start();
 	       
     }
  }

 

 

 

Other Styles for ProgressBar

  • Widget.ProgressBar.Horizontal

  • Widget.ProgressBar.Small
  • Widget.ProgressBar.Large
  • Widget.ProgressBar.Small.Inverse
  • Widget.ProgressBar.Large.Inverse

 

cs453:mobile programming

  • home
  • outline
  • projects
  • syllabus
  • links