Android development in progress-Use Dialog progressdialog

Source: Internet
Author: User

 

Progressdialog is a subclass of alertdialog. It is used to display the dialog with a progress bar. This progress-based UI control can be seen in many UI frameworks. When a user executes an operation for a long time, in a well-designed system, a progress bar should be displayed to indicate the progress of the user's current processing. The progress bar is the most common in installing applications and network interaction applications. It takes a long time to install an application, so you need to indicate the progress of the user, network interaction also needs to indicate the progress of user interaction (especially when you upload or download large files) due to the unstable network environment ). The progress bar is a commonly used control.

Android provides us with a very easy-to-use built-in progress bar dialog, Which is progressdialog. The progress bar in progressdialog is divided into two types: The progress bar for determining values and the progress bar for undefinable values. The progress bar indicates that the system can indicate the current progress value and must update the value of the current progress bar. If the progress bar reaches a certain value, it indicates the completion progress. The uncertain value progress bar refers to the interface on which the system instructs you to wait, but does not provide the current progress, so you cannot know where the current progress is, but you can see that the system is processing and you still need to wait, therefore, it does not have a definite value.

Let's take a look at the simplest code to display progressdialog:

Progressdialog. Show (this, "loading...", "the system is processing your request "); 

The above Code directly shows a progressdialog, which is a progressdialog of an uncertain value progress bar. Its parameters are simple. The first parameter is context information, and the second parameter is the Title of dialog, the third parameter is the information to be displayed in the dialog body. The effect of the Code is as follows:

You can see that progressdialog shows a small circle that is being rotated. This small circle will rotate permanently until the dialog disappears, so this is an undefinable value progress bar. When running the code, we find that the dialog cannot be canceled. It will always exist until you use code control to make the dialog disappear, so it cannot be canceled. If you want to cancel the dialog, the following two parameters are added:

Progressdialog. show (this, <br/> "loading... ", // Title <br/>" the system is processing your request ", // content <br/> true, // undefinable value <br/> true ); 

 

The progressdialog created using the show method is relatively simple. It is troublesome to create a progress bar dialog with a definite value, because you need to write some additional code to control the progress bar. To create progressdialog, you need to create a new object through new, which can be generated through configuration without builder. So what we need to do is:

  1. Create a progressdialog object
  2. Setting the internal progress bar is deterministic.
  3. Display it
  4. Update the progress bar value. After the progress is complete, hide the dialog box.

 

Based on the preceding steps, we can create a reusable ssdialog with deterministic values. First, we need to assign an id value to the dialog to be managed, and then rewrite the oncreatedialog method to create progressdialog.

@ Override <br/> protected dialog oncreatedialog (int id) {</P> <p> switch (ID) {</P> <p> case progress_dialog: <br/> progressdialog Pd = new progressdialog (this); <br/> PD. settitle ("progress bar"); // set the title <br/> PD. setmessage ("processing... please wait... "); // set the body Information <br/> PD. setmax (100); // The maximum progress bar value is 100 <br/> PD. setprogressstyle (progressdialog. style_horizontal); // set the progress bar style to horizontal </P> <p> return PD; </P> <p >}</P> <p> return Super. oncreatedialog (ID); <br/>} 

Here we override the oncreatedialog method of the activity to make progressdialog a reusable managed dialog. When the button is clicked, we call:

Showdialog (progress_dialog ); 

Display the progressdialog object created in the oncreatedialog method:

The above figure shows the effect of the progress bar progressdialog, but its progress bar won't change. This is because you haven't used the code to control the progress bar, so we need to write some additional code here, when the form is displayed, first let the progress bar return to zero, and then use a thread to increase the progress bar by 1 every MS, so it takes 10 seconds to read the entire progress bar. We use handler objects for processing. Because Android uses the UI single-thread mode, you cannot access the UI control by creating your own thread. Otherwise, an exception is thrown. Handler puts the code to be executed into the UI thread for execution. Therefore, we first define a handler internal class to change the value of the progress bar.

Private Static final int progress_dialog = 1; </P> <p> int progressvalue = 0; // Save the current progress value of the progress bar, when the maximum value is reached, hide progressdialog <br/> progressdialog mprogressdialog; // The referenced progressdialog object <br/> handler progresshandler; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> setcontentview (R. layout. dialog3); </P> <p> progresshandler = new handler () {</P> <p> Public void handlemessage (Android. OS. message MSG) {</P> <p> If (progressvalue = 100) {<br/> mprogressdialog. dismiss (); <br/>}else {<br/> // Add 1 to the progress bar value <br/> progressvalue ++; <br/> mprogressdialog. incrementprogressby (1); <br/> // if the progress bar is not over, add 1 to the progress bar after ms, and Add 1 to the progress bar for every MS of cyclic calls <br/> progresshandler. sendemptymessagedelayed (0,100); <br/>}</P> <p >}; </P> <p>} 

Then, add a button event. When the user clicks progressdialog, the progress bar is cleared by 0, and the first handler message to start updating the progress bar is sent, because the handler Message Processing Method sends messages cyclically, as long as the program is triggered for the first time, an update message is sent every 100 ms until the progress bar value reaches. the dialog box is hidden. All code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout <br/> xmns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> </P> <p> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "display the simplest progressdialog" <br/> Android: onclick = "onclick1" <br/> </P> <p> <button <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "ssdialog with a definite value" <br/> Android: onclick = "onclick2" <br/> </P> <p> </linearlayout> 

Package COM. king. dialog. uicontroller. dialog; </P> <p> Import COM. king. dialog. uicontroller. r; </P> <p> Import android. app. activity; <br/> Import android. app. dialog; <br/> Import android. app. progressdialog; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. view. view; </P> <p> public class progressdialogactivity extends activity {</P> <p> Private Static final int progress_dialog = 1; </P> <p> int progressvalue = 0; // Save the current progress value of the progress bar. When the maximum value is reached, hide progressdialog <br/> progressdialog mprogressdialog; // The referenced progressdialog object <br/> handler progresshandler; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> setcontentview (R. layout. dialog3); </P> <p> progresshandler = new handler () {</P> <p> Public void handlemessage (Android. OS. message MSG) {</P> <p> If (progressvalue = 100) {<br/> mprogressdialog. dismiss (); <br/>}else {<br/> // Add 1 to the progress bar value <br/> progressvalue ++; <br/> mprogressdialog. incrementprogressby (1); <br/> // if the progress bar is not over, add 1 to the progress bar after ms, and Add 1 to the progress bar for every MS of cyclic calls <br/> progresshandler. sendemptymessagedelayed (0,100); <br/>}</P> <p >}; </P> <p> }; </P> <p >}</P> <p> Public void onclick1 (view) {</P> <p> progressdialog. show (this, <br/> "loading... ", // Title <br/>" the system is processing your request ", // content <br/> true, // undefinable value <br/> true ); // cancel <br/>}</P> <p> @ override <br/> protected dialog oncreatedialog (int id) {</P> <p> switch (ID) {</P> <p> case progress_dialog: <br/> mprogressdialog = new progressdialog (this ); <br/> mprogressdialog. settitle ("progress bar"); // set the title <br/> mprogressdialog. setmessage ("processing... please wait... "); // set the body Information <br/> mprogressdialog. setmax (100); // maximum progress bar value is 100 <br/> mprogressdialog. setprogressstyle (progressdialog. style_horizontal); // set the progress bar style to horizontal </P> <p> return mprogressdialog; </P> <p >}</P> <p> return Super. oncreatedialog (ID); <br/>}</P> <p> Public void onclick2 (view) {</P> <p> showdialog (progress_dialog ); <br/> progressvalue = 0; <br/> mprogressdialog. setprogress (0); <br/> progresshandler. sendemptymessage (0); </P> <p >}</P> <p>} 

The code runs as follows:

You can see the progress bar that is moving. When the progress bar reaches 100, progressdialog automatically disappears. One difficulty here is the handler usage. I will introduce it in detail later. Now I want to know handler first, that is, I can put the code into the UI thread for execution.

 

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.