ProgressDialog (Dialog progress bar) by implementing thread Update in Android

Source: Internet
Author: User

As a developer we often need to stand on the user's point of view, such as when the App store download software, when the user clicks the download button, there will be a download Progress prompt page appears, now we are through the thread hibernation simulation download Progress update demo, (here for the convenience of setting up the dialog progress bar on the screen):

Layout Interface code (deploy only one button:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <linearlayout xmlns:android= "/http Schemas.android.com/apk/res/android " 3     android:orientation=" vertical "android:layout_width=" Match_ Parent " 4     android:layout_height=" Match_parent "> 5     <Button  6         android:layout_width= "Wrap_content" 7         android:layout_height= "wrap_content" 8         android:text= "Download"// Real Project recommended uniform definition of text resources in Strings.xml under Res 9         Android:o nclick= "Begin"/> </LinearLayout>

Java Code implementation (progress update via process):

1  Public classProgressbardemoextendsappcompatactivity {2 @Override3     protected voidonCreate (@Nullable Bundle savedinstancestate) {4         Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.progressbar);6     }7      Public voidbegin (View v) {8         //Instantiate progress Bar dialog box (progressdialog)9         FinalProgressDialog PD =NewProgressDialog ( This);TenPd.settitle ("Please wait a moment"); One         //Set dialog progress bar style to horizontal A Pd.setprogressstyle (progressdialog.style_horizontal); -         //Set Prompt Information -Pd.setmessage ("Trying to download ..."); the         //Set dialog progress bar to display at the top of the screen (convenient) - Pd.getwindow (). setgravity (gravity.top); -Pd.setmax (100); -Pd.show ();//call the Show method to display the Progress Bar dialog box +         //using anonymous inner classes to implement threads and start -         NewThread (NewRunnable () { +             intinitial = 0;//Initial Download Progress A @Override at              Public voidrun () { -                  while(Initial<pd.getmax ()) {//set up a loop condition -Pd.setprogress (INITIAL+=40);//Set each completion -                     Try { -Thread.Sleep (1000); -}Catch(interruptedexception e) { in e.printstacktrace (); -                     } to                 } +Pd.dismiss ();//dialog box disappears when progress is complete -             } the }). Start (); *     } $}

ProgressDialog (Dialog progress bar) by implementing thread Update 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.