A simple example of ProgressDialog in Android

Source: Internet
Author: User



Examples of how the progress bar is displayed on the web, there is no article on how to close the task at the end, refer to the other article after the experiment to show the complete set of progress bar Simple example is as follows:

Set up Android projects and other jobs are omitted, Google a bit.

The main activity is described below.
Progressbardemo.java


Package Com.lveyo.android.demo.progressbar;

Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.TextView;

Public class Progressbardemo extends Activity {
   
 private TextView statustextview;
 private Button beginbtn;
 private ProgressDialog ProgressDialog;
 
    @Override
    public void onCreate (Bundle savedinstancestate) {
        super.oncreate (savedinstancestate);
        Setcontentview (R.layout.main);
        Statustextview = (TextView) Findviewbyid (r.id.status);
        beginbtn = (Button) Findviewbyid (R.ID.BEGINBTN);
        Setlistener ();
   }
   
   /**
     * Update UI with Handler
      */
    private Handler Handler = new Handler () {

   @Override
  public void Handlemessage (Message msg) {
   
    //Close ProgressDialog
   progressdialog.dismiss ();
   
   //Update UI
   statustextview.settext ("Completed!");
  }};
   
  
 /**
  * Click button Event Listener
  */
    private void Setlistener () {
     beginbtn.setonclicklistener (New View.onclicklistener () {
   
    @Override
   public void OnClick (View v) {
    &NBSP
    //Display ProgressDialog
    progressdialog = Progressdialog.show (Progressbardemo.this, "Loading ...", "Please wait ...", true, false);
    
    //new Thread
    new thread () {

@Override
public void Run () {
Methods that take time to calculate
Calculation.calculate (4);

Send a message to handler
Handler.sendemptymessage (0);
}}.start ();
}
});
}

}



Package Com.lveyo.android.demo.progressbar;

/**
* Schematic method
* @author Lveyo
*
*/
public class Calculation {

public static void calculate (int sleepseconds) {
try {
Thread.Sleep (sleepseconds * 1000);
} catch (Exception e) {
Todo:handle exception
}
}

}


<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview android:id= "@+id/status"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button android:id= "@+id/beginbtn"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Begin"
/>
</LinearLayout>


In Android, we usually can't update the UI in a separate thread, but in the main thread, which is why we're using handler, and when handler receives the message, it puts it in the queue and waits for execution, which is usually done quickly.




A simple example of ProgressDialog in Android

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.