Various progress bars for Android (progressbar)

Source: Internet
Author: User

When an application is executed in the background, the foreground interface does not have any information, and the user does not knowProgramWhether the program is being executed, how the execution progress is, and whether the application is terminated due to an error. You need to use a progress bar to display the progress of the background program execution. The Android system provides two types of progress bar styles and a long progress bar (Progress-barstylehorizontal)
And the circular progress bar (progressbarstylelarge ). Progress bars are useful. For example, when an application loads resources and connects to a network, you can be prompted to wait. These progress bars only represent the execution of a certain part of the application, however, you can display a progress bar through the Application title bar. Therefore, you need to set the window display style "requestwindowfeature (window. feature_progress )".
Let's take a look at the running effect of this sample program.

 

First layout the file:

 1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" 3 Android: Orientation = "vertical" 4 Android: layout_width = "fill_parent"5 Android: layout_height = "fill_parent" 6 > 7 < Textview  8 Android: layout_width = "fill_parent" 9 Android: layout_height = "wrap_content" 10 Android: text = "@ string/hello" 11 /> 12   13 < Progressbar  14 Android: Id = "@ + ID/progressbar01" 15 Style = "? Android: ATTR/progressbarstylehorizontal" 16 Android: layout_width = "200dp" 17 Android: layout_height = "wrap_content" 18 Android: visibility = "gone" 19 /> 20   21 < Progressbar  22 Android: Id = "@ + ID/progressbar02" 23 Style = "? Android: ATTR/progressbarstylelarge" 24 Android: layout_width = "wrap_content" 25 Android: layout_height = "wrap_content" 26 Android: max = "100" 27 Android: Progress = "50" 28 Android: secondaryprogress = "70" 29 Android: visibility = "gone" 30 /> 31   32 < Button  33 Android: Id = "@ + ID/button01" 34 Android: layout_width = "wrap_content"35 Android: layout_height = "wrap_content" 36 Android: text = ""/> 37   38 </Linearlayout>

Progressbar01 is a long progress bar and progress02 is a circular progress bar. The title bar progress bar is not declared here. "Style =" indicates the style of the progress bar. The following sets the maximum value of the progress bar through the setmax method, the setprogress method sets the current value of the progress bar, And the setvisibility method sets the visibility of the progress bar.

 

 Package  Xiaohang. zhimeng;  Import  Android. App. activity;  Import  Android. OS. Bundle;  Import Android. OS. Handler;  Import  Android. OS. message;  Import  Android. View. view;  Import  Android. View. window;  Import  Android. widget. Button;  Import  Android. widget. progressbar;  Public   Class Activity01 Extends  Activity { //  Declare a progressbar object          Private  Progressbar xh_progressbar;  Private  Progressbar xh_progressbar2;  Private  Button xh_button;  Protected   Static   Final   Int Gui_stop_notifier = 0x108 ;  Protected   Static  Final   Int Gui_threading_notifier = 0x109 ;  Public   Int Intcounter = 0 ; @ Override  Public   Void  Oncreate (bundle savedinstancestate ){  Super  . Oncreate (savedinstancestate );  //  Set window mode, because the progress bar needs to be displayed in the title bar Requestwindowfeature (window. feature_progress); setprogressbarvisibility (  True  ); Setcontentview (R. layout. Main );  //  Get progressbar Xh_progressbar = (Progressbar) findviewbyid (R. Id. progressbar01); xh_progressbar2 = (Progressbar) findviewbyid (R. Id. progressbar02); xh_button = (Button) findviewbyid (R. Id. button01 );  //  Set the progress bar to the uncertain Mode Xh_progressbar.setindeterminate ( False  ); Xh_progressbar2.setindeterminate (  False  );  //  Start execution when the button is pressed Xh_button.setonclicklistener ( New  Button. onclicklistener () {@ override  Public   Void  Onclick (view v ){  // Set progressbar to visible  Xh_progressbar.setvisibility (view. Visible); xh_progressbar2.setvisibility (view. Visible );  //  Set the maximum value of progressbar Xh_progressbar.setmax (100 );  //  Set the current value of progressbar Xh_progressbar.setprogress (0 ); Xh_progressbar2.setprogress ( 0 ); //  Use a thread to change the value of progressbar                                  New Thread ( New  Runnable () {@ override  Public   Void  Run (){  For ( Int I = 0; I <10; I ++ ){  Try {  //  Set progress Value Intcounter = (I + 1) * 20 ;  //  Sleep 1000 milliseconds Thread. Sleep (1000 );  If (I = 4 ) {Message m =New  Message (); M. What = Activity01.gui _ stop_notifier; activity01.  This  . Mymessagehandler. sendmessage (m );  Break  ;}  Else {Message m = New  Message (); M. What = Activity01.gui _ threading_notifier; activity01.  This  . Mymessagehandler. sendmessage (m );}}  Catch (Exception e) {e. printstacktrace () ;}}}). Start () ;}});} handler mymessagehandler = New  Handler () {@ override  Public   Void  Handlemessage (Message MSG ){  Switch  (Msg. What ){ //  Progressbar is already the maximum value                          Case  Activity01.gui _ stop_notifier: xh_progressbar.setvisibility (view. Gone); xh_progressbar2.setvisibility (view. Gone); thread. currentthread (). interrupted ();  Break  ;  Case  Activity01.gui _ threading_notifier:  If (!Thread. currentthread (). isinterrupted ()){  //  Change the current value of progressbar  Xh_progressbar.setprogress (intcounter); xh_progressbar2.setprogress (intcounter );  //  Set a progress bar value in the title bar. Setprogress (intcounter * 100 );}  Break  ;} Super  . Handlemessage (MSG );}};} 

 

 

 

Http://bbs.droidstouch.com/thread-105-1-3.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.