Introduction to Android Development (10) Basic Controls 10.3 ProgressBar

Source: Internet
Author: User
Tags thread

ProgressBar provides a visual feedback when performing certain tasks that are being processed. For example, you are downloading data from a Web server, and then you need to update the status of the download. In this case, ProgressBar is a good choice. The following example shows how to use ProgressBar.

1. Create a project, BasicViews2.

2. Code in the Main.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=     
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
    android:o" rientation= "vertical" >     
         
<progressbar android:id= "@+id/progressbar" android:layout_width= "WRAP_" 
    Content "  
    android:layout_height=" wrap_content "/>     
         
</LinearLayout>

3, the code in the Basic2activity.java.

public class Basicviews2activity extends activity {static int progress;     
    ProgressBar ProgressBar;     
    int progressstatus = 0;     
         
    Handler Handler = new Handler (); /** called the activity is a.     
        * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);     
                 
        Setcontentview (R.layout.main);     
        progress = 0;     
                 
        ProgressBar = (ProgressBar) Findviewbyid (R.id.progressbar); ---do some work in background thread---new Thread (new Runnable () {public VO     
                ID run () {//?do Some work here?     
         
                while (Progressstatus <) {progressstatus = Dosomework ();     
   }//---Hides the progress bar---             Handler.post (New Runnable () {public void run () {//---0-visible; 4-invisible 8-gone---progres     
                    Sbar.setvisibility (View.gone);     
            }     
                });      
            }//---Do some long running work---private int dosomework () {try {//---simulate doing some work---thre     
                Ad.sleep (500);     
                catch (Interruptedexception e) {e.printstacktrace ();     
            return ++progress;     
    }). Start (); }     
}

4, F11 debugging, you will see ProgressBar animation, 5 Seconds later, the animation disappears.

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.