ProgressBar provides a visual feedback when performing certain tasks that are being processed. For example, you are downloading data from a Web server, and then you need to update the status of the download. In this case, ProgressBar is a good choice. The following example shows how to use ProgressBar.
1. Create a project, BasicViews2.
2. Code in the Main.xml.
<?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:o" rientation= "vertical" >
<progressbar android:id= "@+id/progressbar" android:layout_width= "WRAP_"
Content "
android:layout_height=" wrap_content "/>
</LinearLayout>
3, the code in the Basic2activity.java.
public class Basicviews2activity extends activity {static int progress;
ProgressBar ProgressBar;
int progressstatus = 0;
Handler Handler = new Handler (); /** called the activity is a.
* * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
progress = 0;
ProgressBar = (ProgressBar) Findviewbyid (R.id.progressbar); ---do some work in background thread---new Thread (new Runnable () {public VO
ID run () {//?do Some work here?
while (Progressstatus <) {progressstatus = Dosomework ();
}//---Hides the progress bar--- Handler.post (New Runnable () {public void run () {//---0-visible; 4-invisible 8-gone---progres
Sbar.setvisibility (View.gone);
}
});
}//---Do some long running work---private int dosomework () {try {//---simulate doing some work---thre
Ad.sleep (500);
catch (Interruptedexception e) {e.printstacktrace ();
return ++progress;
}). Start (); }
}
4, F11 debugging, you will see ProgressBar animation, 5 Seconds later, the animation disappears.