The progress bar of Android includes the dialog box progress bar, title progress bar, and horizontal progress bar.
1. Dialog progress bar
Procedure
1. overwrite the oncreatedialog () method of the activity and create a dialog box in it.
2. Call the showdialog () method of the activity to display the progress bar dialog box.
/Chapter04_ui_progressbar01/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. App. Dialog;
Import Android. App. progressdialog;
Import Android. content. dialoginterface;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public Class Mainactivity Extends Activity {
Private Button mybtn;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Mybtn = (Button) findviewbyid (R. Id. button01 );
Mybtn. setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Showdialog ( 0 );
}
});
}
@ Override
Protected Dialog oncreatedialog ( Int ID ){
Progressdialog Dialog = New Progressdialog ( This );
// The title is not displayed.
Dialog. settitle ( " Test dialog box " );
Dialog. setindeterminate ( True );
Dialog. setmessage ( " ProgramLoading... please wait! " );
Dialog. setcancelable ( True );
Dialog. setbutton (dialog. button_positive, " OK " ,
New Dialoginterface. onclicklistener (){
@ Override
Public Void Onclick (dialoginterface dialog, Int Which ){
Dialog. Cancel ();
}
}
);
Return Dialog;
}
}
Ii. Title Bar progress bar
1. Call the requestwindowfeatures () method of the activity to obtain the progress bar.
2. Call the setprogressbarindeterminatevisibility () method of the activity to display the progress bar dialog box.
/Chapter04_ui_progressbar02/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. window;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public Class Mainactivity Extends Activity {
/** Called when the activity is first created. */
Private Button B1, B2;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Requestwindowfeature (window. feature_indeterminate_progress );
Setcontentview (R. layout. Main );
B1 = (Button) findviewbyid (R. Id. button01 );
B2 = (Button) findviewbyid (R. Id. button02 );
B1.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Setprogressbarindeterminatevisibility ( True );
}
});
B2.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Setprogressbarindeterminatevisibility ( False );
}
});
}
}
3. Horizontal progress bar
Procedure
1. Declare progressbar in the layout file.
2. Obtain the progressbar instance in the activity.
3. Call the incrementprogressby () method of progressbar to increase or decrease the progress.
/Chapter04_ui_progressbar03/src/COM/Amaker/test/mainactivity. Java
Code
Package Com. Amaker. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. progressbar;
Public Class Mainactivity Extends Activity {
Private Button B1, B2;
Progressbar;
@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
B1 = (Button) findviewbyid (R. Id. button01 );
B2 = (Button) findviewbyid (R. Id. button02 );
Progressbar = (Progressbar) findviewbyid (R. Id. progressbar01 );
B1.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Progressbar. incrementprogressby ( 1 );
}
});
B2.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Progressbar. incrementprogressby ( - 1 );
}
});
}
}
/Chapter04_ui_progressbar03/RES/layout/Main. xml
Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
Android: Orientation = "Vertical"
Android: layout_width = "Fill_parent"
Android: layout_height = "Fill_parent"
>
< Progressbar
Android: ID = "@ + ID/progressbar01"
Style = "? Android: ATTR/progressbarstylehorizontal"
Android: layout_width = "200dip"
Android: layout_height = "Wrap_content"
Android: Max = "100"
Android: Progress = "50"
> </ Progressbar >
< Button
Android: ID = "@ + ID/button01"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "Add" > </ Button >
< Button
Android: ID = "@ + ID/button02"
Android: layout_width = "Wrap_content"
Android: layout_height = "Wrap_content"
Android: Text = "Few" > </ Button >
</ Linearlayout >