Parsing the usage of ProgressBar in Android _android

Source: Internet
Author: User
Tags visibility

The

Example Description
widget for Android is specifically designed to interact with the user, but it is also part of a program prompt that displays the running state of a program widget. The examples presented here are similar to those used in the previous chapter of the ProgressDialog dialog box, However, because the ProgressDialog introduced in the previous chapter is the interactive dialogue window that inherits from Android.app.ProgressDialog, when applying, you must create a new ProgressDialog object, which will pop up "dialog box" as a reminder at run time. At this point, the application background lost focus, until the process is over, will control to the application, if the activity does not want the background, and hope that the user has a background program is busy stage, at this time, ProgressBar will come in handy.

The ProgressBar widget control provided by Android is different from the ProgressDialog application target, which can be laid out in main.xml layout at the beginning of the program. First, set the properties of the ProgressBar deployed in layout as hidden (unseen at first), then use the process to "pretend" the program is busy, but the difference is, you can get the progress of the runtime in the process, in the "Run" process, the running progress through the TextView display. In addition to learning the display and use of the ProgressBar widget, the other learning key is the use of handler because the new process cannot access the widget in the activity or send out the running state. So it is necessary to pass the state of the process to the handler and the Message object, and finally to receive the running state by the activity's handler event.

Sample Program
Src/irdc.ex04_17/ex04_17.java
In order for thread to run, the information can be continuously passed to the activity, so the Android.os.Handler object and the Android.os.Message object are used, and two integers are declared in the class member variable: GUI_STOP_ NOTIFIER and Gui_threading_notifier, the two integers will be identified as signals when the thread needs to be stopped, which is the identity to be processed during the process being run.

The program designed a button, this button is the work of the original deployment in the Main.xml ProgressBar display (originally set to android:visibility= "Gone"), And because the default does not specify its indeterm-inate attribute in Main.xml, the Setindeterminate () method of ProgressBar is invoked even in the program. You cannot change the value of progressbar.getprogress (), which will always be 0. Therefore, the author wants to use the circular picture animation as the animation in the running process, and uses a counter (integer) to increment, represents the running percentage.

Copy Code code as follows:

/* Import procedures slightly * *

public class Ex04_17 extends activity
{
Private TextView mTextView01;
Private Button MButton01;
Private ProgressBar MProgressBar01;
public int intcounter=0;

/* Custom handler information code, to be used as an identity event handling * *
protected static final int gui_stop_notifier = 0x108;
protected static final int gui_threading_notifier = 0x109;

/** called the activity is a. */
@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 ProgressBar Widget Object * *
MProgressBar01 = (ProgressBar) Findviewbyid (R.ID.MYPROGRESSBAR1);

/* Call Setindeterminate Method Assignment indeterminate mode to False * *
Mprogressbar01.setindeterminate (FALSE);

* * When the button is clicked, start the process work/*
Mbutton01.setonclicklistener (New Button.onclicklistener ()
{
@Override
public void OnClick (View v)
{
TODO auto-generated Method Stub

/* Click the button to let ProgressBar display * *
Mtextview01.settext (R.string.str_progress_start);

* * To display the hidden ProgressBar * *
Mprogressbar01.setvisibility (view.visible);

/* Specify progress for up to 100 * *
Mprogressbar01.setmax (100);

/* Initial progress is 0 * *
Mprogressbar01.setprogress (0);

/* Start a process * *
New Thread (New Runnable ()
{
public void Run ()
{
/* Default 0 to 9, running a total of 10 times the LOOP statement * * *
for (int i=0;i<10;i++)
{
Try
{
/* member variable, to identify the loading progress * *
Intcounter = (i+1) *20;
/* Every run cycle, that is, pause for 1 seconds * *
Thread.Sleep (1000);

* * When thread is running for 5 seconds, show run end.
if (i==4)
{
/* To Message object, pass parameters to handler * *
Message m = new Message ();

/* Specify user customization with the What property */
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 ();
}
});
}

* * Handler after the build, will listen to the message code * *
Handler Mymessagehandler = new Handler ()
{
@Override
public void Handlemessage (msg)
{
Switch (msg.what)
{
* * When the information obtained is identified as leaving the process
Case Ex04_17.gui_stop_notifier:

/* Show End of Run * *
Mtextview01.settext (R.string.str_progress_done);

/* Set ProgressBar widget for hidden */
Mprogressbar01.setvisibility (View.gone);
Thread.CurrentThread (). interrupt ();
Break

* * When obtaining information that is identified as continuing in the process
Case Ex04_17.gui_threading_notifier:
if (! Thread.CurrentThread (). isinterrupted ())
{
Mprogressbar01.setprogress (intcounter);
/* will show 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 sample program, call Mprogressbar01.setindeterminate (false), do not show background progress bar, if set to Mprogressbar01.setindeterminate (true), It is also not possible to have the default ProgressBar picture (circle) have the correct progress hint, because the default ProgressBar does not support indeterminate mode loop image, so even if setin-determinate (true) Also does not display progress correctly. In this program, in order to deliberately write as a control exercise, generally in the unknown "progress" in the case, you can use text to display the progress percentage, by calling Mprogressbar01.getprogress () to get the running progress value, displayed in the text. Please add a android:indeterminateonly property to the ProgressBar widget definition in layout, specifying that its value is false and does not show background progress bar.
the following are as follows:
Copy Code code 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"
/>

ProgressBar In addition to the above about Android:progressbarstyle property settings, the author also investigated the online Android source code (http:// source.android.com), some of the original Android used Progressbarstylehori-zontal properties, in addition to the default "round" picture, there are other themes and square pictures drawable mode can be used.
Copy Code code as follows:

<resources>
<declare-styleable name= "Theme" >
<!--snip-->
<attr name= "progressbarstylehorizontal" format= "Reference"/>
</resources>

Next look, the attribute name Progressbarstylehorizontal in this topic is defined in Frameworks/base/core/res/res/values/styles.xml, as follows:
Copy Code code as follows:

<resources>
<style name= "Widget.ProgressBar.Horizontal" >
<item name= "Android:indeterminateonly" >false</item>
<item name= "Android:progressdrawable" >
@android:d Rawable/progress_horizontal
</item>
<item name= "Android:indeterminatedrawable" >
@android:d Rawable/progress_indeterminate_horizontal
</item>
<item name= "Android:minheight" >20dip</item>
<item name= "Android:maxheight" >20dip</item>
</style>
</resources>

This shows that if you want Android to use other styles of ProgressBar, you can add the following two properties to the original layout (main.xml) to see how the picture changes during the run.
Copy Code code as follows:

android:progressdrawable= "@android:d rawable/progress_horizontal"
android:indeterminatedrawable=
"@android:d rawable/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.