Android advanced components ----- progress bar, Android ----- progress bar

Source: Internet
Author: User

Android advanced components ----- progress bar, Android ----- progress bar

The progress bar component is one of the important Android components. When a program is executed in the background, the foreground progress bar displays the percentage of the program execution progress dynamically. It is a time-consuming program that allows users to feel in their control and improve program-friendly.

Android supports several styles of progress bars. You can set the progress bar style through the style attribute, which is as follows:

@ Android: style/Widget. ProgressBar. Horizontal progress bar

@ Android: style/Widget. ProgressBar. Inverse General ring progress bar

@ Android: style/Widget. ProgressBar. Large ring progress bar

@ Android: style/Widget. ProgressBar. Large. Inverse Large ring progress bar

@ Android: style/Widget. ProgressBar. Small ring progress bar

@ Android: style/Widget. ProgressBar. Small. Inverse Small ring progress bar

In addition, the progress bar also has the following attributes in the xml file:

Android: max sets the maximum value of the progress bar

Android: rpogress specifies the progress value of the progress bar that has been completed

Android: ProgressDrawable

The progress bar component has two most common methods: setProgress () to set the progress of the progress bar that has been completed; incrementProgressBy () to set the progress bar to increase or decrease; the parameter is increased when a positive number increases, and the negative number decreases;

Instance operation: The following shows the progress bar and the ring progress bar.

1. Create a project, add two progress bars to the layout, one being a ring and the other being a horizontal progress bar.

            <ProgressBar            android:id="@+id/progressBar1"            style="?android:attr/progressBarStyleHorizontal"            android:layout_gravity="center_horizontal"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:max="100"            android:layout_weight="1" />        <ProgressBar            android:id="@+id/progressBar2"            style="?android:attr/progressBarStyleSmall"            android:layout_gravity="center_horizontal"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1" />

2. Get the progress bar in the main activity and define a progress variable and a Handler object for message processing.

Hp = (ProgressBar) findViewById (R. id. progressBar1); cp = (ProgressBar) findViewById (R. id. progressBar2); h = new Handler () {@ Override public void handleMessage (Message msg) {if (msg. what = 0x11) {hp. setProgress (progress);} else {Toast. makeText (MainActivity. this, "time-consuming completion", Toast. LENGTH_SHORT ). show ();} hp. setVisibility (View. GONE); cp. setVisibility (View. GONE );}};

3. Use an anonymous internal class to instantiate a message to process the Handler class object, and override the handlerMessage () method. The update progress is displayed when the time consumption is not completed.

H = new Handler () {public void handleMessage (Message msg) {if (msg. what = 0x11) {hp. setProgress (progress);} else {Toast. makeText (MainActivity. this, "time-consuming completion", Toast. LENGTH_SHORT ). show ();} hp. setVisibility (View. GONE); cp. setVisibility (View. GONE );}}

4. When thread time is enabled, sendMessage () sends and processes messages.

        new Thread(new Runnable() {                        @Override            public void run() {                while(true){                    progress = doWork();                    Message m = new Message();                    if(progress <= 100){                        m.what = 0x11;                        h.sendMessage(m);                    }else {                        m.what = 0x10;                        h.sendMessage(m);                        break;                    }                }                            }                        private int doWork(){                progress += Math.random()*10;                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                return progress;            }        }).start();

Run the code to view the ring progress bar and horizontal progress bar update.

 

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.