SoFunction
Updated on 2025-04-10

Simple example of the usage of ProgressBar in Android

This article describes the usage of ProgressBar in Android. Share it for your reference, as follows:

ProgressBar is often used in Android. The following is an example to illustrate how to use ProgressBar.

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class A03Activity extends Activity {
 private ProgressBar rectangle,circle;
 private Button showProgressBar;
 private final static int STOP=0x10000;
 private final static int NEXT=0x10001;
 private int count=0;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    rectangle=(ProgressBar)findViewById();
    circle=(ProgressBar)findViewById();
    showProgressBar=(Button)findViewById();
    (false);
    (false);
    (new OnClickListener(){
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  ();
  ();
  (100);
  (0);
  (0);
  Thread t=new Thread(new Runnable(){
   @Override
   public void run() {
   // TODO Auto-generated method stub
   for(int i=0;i<20;i++){
    try {
    count=(i+1)*5;
    (1000);
    if(count==19){
     Message msg=new Message();
     =STOP;
     (msg);
     break;
    }
    else{
     Message msg=new Message();
     =NEXT;
     (msg);
    }
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    ();
    }
   }
   }   
  });
  ();
  }
  });
  }
  private Handler handler=new Handler(){
   @SuppressWarnings("static-access")
 public void handleMessage(Message msg){
   switch(){
   case STOP:
    ();
    ();
    ().interrupt();
    break;
   case NEXT:
    if(!().interrupted()){
    (count);
    (count);
    }
    break;
   }
   }
  };
}

res/layout/ as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />
  <ProgressBar 
    android:
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleHorizontal"
    mce_style="?android:attr/progressBarStyleHorizontal"
    android:visibility="gone"
    />
  <ProgressBar 
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/progressBarStyleLarge"
    mce_style="?android:attr/progressBarStyleLarge"
    android:visibility="gone"    
    />
  <Button 
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="show ProgressBar"
    />
</LinearLayout>

For more information about Android controls, please visit the special topic of this site:Android control usage summary

I hope this article will be helpful to everyone's Android programming design.