Parse the usage of ProgressBar in android

Source: Internet
Author: User

Example
Many Android widgets are specially designed to interact with users, but some are widgets that are used as program prompts and display the running status of programs. The example is similar to the Application Purpose of the ProgressDialog dialog box introduced in the previous chapter. However, the ProgressDialog introduced in the previous chapter inherits from Android. app. in the Interactive dialog window designed by ProgressDialog, you must create a ProgressDialog object during the application. A "dialog box" will pop up at runtime as a reminder. At this time, the background of the application loses focus until the process ends, will be handed over to the application. If you do not want the background to lose focus in the Activity, and want to prompt the User that a background program is in a busy stage, at this time, progressBar will be used.

The ProgressBar Widget control provided by Android is different from the ProgressDialog application target. in the xml Layout, the ProgressBar attribute deployed in Layout is set to hidden (invisible at first), and then the process is used to "Pretend" that the program is busy, but the difference is that, you can get the running Progress in the process. During the "running" process, the running Progress is displayed through TextView. In this example, in addition to the display and use of the ProgressBar Widget, another learning key is Handler's use, because the new process cannot access the Widget in the Activity or send the running status out, therefore, the Handler and Message object must be used to pass the status in the process out, and finally the Handler event of the Activity receives the running status.

Sample program
Src/irdc. ex04_17/EX04_17.java
To enable the Thread to continuously pass information to the Activity during running, Android is used. OS. handler object and Android. OS. message object, and two integers are declared in the class member variables: GUI_STOP_NOTIFIER and GUI_THREADING_NOTIFIER. These two integers are used as the signal identification when the information is transmitted, the former is the identifier to be processed when the Thread needs to stop calling and the latter is the process running.

A button is designed in the program to make the button originally deployed in main. the ProgressBar in xml is displayed (originally set to Android: visibility = "gone. the indeterm-inate attribute is not specified in xml, so even if the setIndeterminate () method of ProgressBar is forcibly called in the program, the ProgressBar cannot be changed. the value of getProgress (), which is always 0. Therefore, the author wants to use the circular image animation as the animation during running, and uses a Counter (integer) to increase, indicating the running percentage.

Copy codeThe Code is as follows:/* import program omitted */

Public class EX04_17 extends Activity
{
Private TextView mTextView01;
Private Button mButton01;
Private ProgressBar mProgressBar01;
Public int intCounter = 0;

/* Custom Handler information code, used as an identifier for event processing */
Protected static final int GUI_STOP_NOTIFIER = 0x108;
Protected static final int GUI_THREADING_NOTIFIER = 0x109;

/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

MButton01 = (Button) findViewById (R. id. myButton1 );
MTextView01 = (TextView) findViewById (R. id. myTextView1 );

/* Set the widget ssbar widget object */
MProgressBar01 = (ProgressBar) findViewById (R. id. myProgressBar1 );

/* Call the setIndeterminate method to assign the value to the indeterminate mode to false */
MProgressBar01.setIndeterminate (false );

/* Click the button to start the process */
MButton01.setOnClickListener (new Button. OnClickListener ()
{
@ Override
Public void onClick (View v)
{
// TODO Auto-generated method stub

/* Click the button to display the ssssbar */
MTextView01.setText (R. string. str_progress_start );

/* Display the hidden ProgressBar */
MProgressBar01.setVisibility (View. VISIBLE );

/* Specify a maximum of 100 SS */
MProgressBar01.setMax (100 );

/* The initial SS is 0 */
MProgressBar01.setProgress (0 );

/* Start a process */
New Thread (new Runnable ()
{
Public void run ()
{
/* The default value is 0 to 9. A total of 10 loop statements are run */
For (int I = 0; I <10; I ++)
{
Try
{
/* Member variables used to identify the loading progress */
IntCounter = (I + 1) * 20;
/* Every running cycle, that is, 1 second is paused */
Thread. sleep (1000 );

/* When the Thread runs for 5 seconds, the running is finished */
If (I = 4)
{
/* Pass the parameter to Handler as a Message object */
Message m = new Message ();

/* Specify User customization with the "what" attribute */
M. what = EX04_17.GUI_STOP_NOTIFIER;
EX04_17.this.myMessageHandler.sendMessage (m );
Break;
}
Else
{
Message m = new Message ();
M. what = EX04_17.GUI_THREADING_NOTIFIER;
EX04_17.this.myMessageHandler.sendMessage (m );
}
}
Catch (Exception e)
{
E. printStackTrace ();
}
}
}
}). Start ();
}
});
}

/* After Handler is built, it will listen to the Information Code sent */
Handler myMessageHandler = new Handler ()
{
// @ Override
Public void handleMessage (Message msg)
{
Switch (msg. what)
{
/* The information obtained when the identity is to leave the process */
Case EX04_17.GUI_STOP_NOTIFIER:

/* Display the end of running */
MTextView01.setText (R. string. str_progress_done );

/* Set the ProgressBar Widget to hide */
MProgressBar01.setVisibility (View. GONE );
Thread. currentThread (). interrupt ();
Break;

/* Information obtained when it is identified as continuous in the process */
Case EX04_17.GUI_THREADING_NOTIFIER:
If (! Thread. currentThread (). isInterrupted ())
{
MProgressBar01.setProgress (intCounter );
/* Display the progress in TextView */
MTextView01.setText
(
GetResources (). getText (R. string. str_progress_start) +
"(" + Integer. toString (intCounter) + "%)/n" +
"Progress:" +
Integer. toString (mProgressBar01.getProgress () +
"/N" + "Indeterminate:" +
Boolean. toString (mProgressBar01.isIndeterminate ())
);
}
Break;
}
Super. handleMessage (msg );
}
};
}

Extended learning
In the example program, mProgressBar01.setIndeterminate (false) is called, and the background progress Bar is not displayed. If it is set to mProgressBar01.setIndeterminate (true), the default progress Bar image cannot be set (circled) there is a correct progress prompt, because the default ProgressBar does not support the indeterminate mode loop image method, so even setIn-determinate (true) cannot display the progress correctly. In this program, in order to deliberately write as a contrast exercise, generally in the case of unknown "progress", you can use the text to display the progress percentage, by calling mProgressBar01.getProgress () get the running Progress value and display it in text. Add the android: indeterminateOnly attribute to the ProgressBar Widget definition in Layout and set the value to false. The background progress Bar is not displayed.
As follows:
Copy codeThe Code is as follows: <ProgressBar
Android: id = "@ + id/myProgressBar1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: progress = "0"
Android: orientation = "horizontal"
Android: progressBarStyle =
"@ Android: style/Widget. ProgressBar. Horizontal"
Android: indeterminateOnly = "false"
Android: visibility = "gone"
/>

In addition to the property settings for Android: progressBarStyle, the author also investigates the source code (http://source.android.com) of online Android, some of the original Android progressBarStyleHori-zontal attributes, in addition to the default "Circular" image, there are other theme and square image Drawable modes available.Copy codeThe Code is as follows: <resources>
<Declare-styleable name = "Theme">
<! -- Snip -->
<Attr name = "progressBarStyleHorizontal" format = "reference"/>
</Resources>

The property name progressBarStyleHorizontal in this topic is defined in frameworks/base/core/res/values/styles. xml, as shown below:Copy codeThe Code is as follows: <resources>
<Style name = "Widget. ProgressBar. Horizontal">
<Item name = "android: indeterminateOnly"> false </item>
<Item name = "android: progressDrawable">
@ Android: drawable/progress_horizontal
</Item>
<Item name = "android: indeterminateDrawable">
@ Android: drawable/progress_indeterminate_horizontal
</Item>
<Item name = "android: minHeight"> 20dip </item>
<Item name = "android: maxHeight"> 20dip </item>
</Style>
</Resources>

Therefore, if you want Android to use another style ProgressBar, you can add the following two attributes in the original Layout (main. xml) to observe the image changes during running.Copy codeThe Code is as follows: android: progressDrawable = "@ android: drawable/progress_horizontal"
Android: indeterminateDrawable =
"@ Android: drawable/progress_indeterminate_horizontal"

Related Article

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.