ProgressBar Android Learning ———— Android Control

Source: Internet
Author: User

ProgressBar

ProgressBar, a progress bar, is a very useful component in the Androidui interface and is typically used to show the user the percentage of a time-consuming operation completed. As a result, it requires a dynamic display of progress, which avoids long periods of time-consuming operations, and makes the user feel that the program is lost, thereby enhancing the friendliness of the interface.

From official documents, to adapt to different application environments, Android has several styles of progress bar built-in, you can set the style of ProgressBar by using the Style property. The following properties are supported, which are shown in the example below:

    • @android: Style/widget.progressbar.horizontal: Horizontal progress bar (can display scale, commonly used).
    • @android: Style/widget.progressbar.small: Small progress bar.
    • @android: Style/widget.progressbar.large: Big progress bar.
    • @android: Style/widget.progressbar.inverse: Constantly jumping, rotating the progress bar of the screen.
    • @android: Style/widget.progressbar.large.inverse: A large progress bar that constantly jumps and rotates animations.
    • @android: Style/widget.progressbar.small.inverse: A small progress bar that constantly jumps and rotates animations.

Only the Widget.ProgressBar.Horizontal style progress bar, can set the progress of the increment, the other style is shown as a looping animation, and set the Widget.ProgressBar.Horizontal style of the progress bar, need to use some properties to set the incremental progress, which Some properties have corresponding setter, getter method, these properties are as follows:

    • Android:max: Sets the maximum value for the progress.
    • Android:progress: Sets the current first progress value.
    • Android:secondaryprogress: Sets the current second progress value.
    • Android:visibility: Sets whether the display is displayed by default.

For Widget.ProgressBar.Horizontal-style progress bars, you can dynamically set the amount of movement in your code, in addition to using the setprogress (int) method, Android also provides us with another Incrementprogressby (int) method, which differs fundamentally from setprogress (int) in that setprogress (int) is directly setting the current progress value, Incrementprogressby (int) is the increment that sets the current progress value (positive numbers are incremented and negative numbers are minus). There are also setsecondaryprogress (int) and Incrementsecondaryprogressby (int) methods that correspond to setprogress (int) and Incrementprogressby (int). Used to set the second progress value.

The following is an XML layout:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" > "

<textview android:id= "@+id/textviewhor"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Widget.Progressbar.Horizontal"/>

<progressbar
Android:id= "@+id/progessbarhor"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Style= "@android: Style/widget.progressbar.horizontal"
android:max= "100"
Android:progress= "20"
Android:secondaryprogress= "40"
android:visibility= "Visible"/>

<textview android:id= "@+id/textviewsmall"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Widget.Progressbar.Small"/>

<progressbar android:id= "@+id/progressbarsmall"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Style= "@android: Style/widget.progressbar.small"/>

<textview android:id= "@+id/textviewlarge"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Widget.Progressbar.Large"/>

<progressbar android:id= "@+id/progressbarlarge"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Style= "@android: Style/widget.progressbar.large"/>

<textview android:id= "@+id/textviewinverse"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Widget.Progressbar.Inverse"/>

<progressbar android:id= "@+id/progressbarinverse"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Style= "@android: Style/widget.progressbar.inverse"/>

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Horizontal" >

<button android:id= "@+id/buttonadd"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "+"/>

<button android:id= "@+id/buttonrdc"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "30DP"
android:text= "-"/>

<button android:id= "@+id/buttonvsb"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "30DP"
android:text= "Visible"/>

</LinearLayout>

</LinearLayout>

Here's the code:

Package com.example.widget;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.ProgressBar;

public class Mainactivity extends Activity {

Private ProgressBar Pbhor, pblarge;//Declaration progress bar Control
Private button Btadd, btreduce, btvisible;//Declaration button control

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_progessbar);

Get control
Pbhor = (ProgressBar) Findviewbyid (R.id.progessbarhor);
Pblarge = (ProgressBar) Findviewbyid (R.id.progressbarlarge);
Btadd = (Button) Findviewbyid (R.id.buttonadd);
Btreduce = (Button) Findviewbyid (R.ID.BUTTONRDC);
Btvisible = (Button) Findviewbyid (R.ID.BUTTONVSB);

Click to disappear Progressbar.large control
Btvisible.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
if (pblarge.getvisibility () = = view.visible) {
Pblarge.setvisibility (View.gone);
} else {
Pblarge.setvisibility (view.visible);
}
}
});

Click to increase or decrease horizontal progress
Button.onclicklistener Buttonlistener = new Button.onclicklistener () {

public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.buttonadd:
if (Pbhor.getprogress () < 90) {
Pbhor.setprogress ((int) (pbhor.getprogress () * 1.2));
}
if (pbhor.getprogress () < 100) {
Pbhor.setsecondaryprogress ((int) (pbhor.getprogress () * 1.2));
}
Break

Case R.ID.BUTTONRDC:
if (pbhor.getprogress () > 10) {
Pbhor.setprogress ((int) (pbhor.getprogress ()/1.2));
}
if (pbhor.getprogress () > 20) {
Pbhor.setsecondaryprogress ((int) (pbhor.getprogress ()/1.2));
}
Break
}
}
};

Btadd.setonclicklistener (Buttonlistener);
Btreduce.setonclicklistener (Buttonlistener);
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}

ProgressBar Android Learning ———— Android Control

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.