Android to update ProgressDialog (Dialog progress bar) by implementing Threads _android

Source: Internet
Author: User

As a developer, we need to constantly think about the problem from the user's perspective, for example, when the App store downloads software, when the user clicks on the download button, there will be download progress prompts the page appears, now we through the thread hibernation way to simulate download progress update demo, such as figure (here for screenshots to facilitate the setting of the dialogue progress bar located above the screen):

Layout Interface code (deploy only one button):

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "android:layout_width=" match_parent "
android:layout_height=" Match_parent ">
<button
android:layout_width=" wrap_content "
android:layout_height=" Wrap_ Content "
android:text=" download//real project It is recommended that the uniform definition of text resources be configured in Strings.xml under
res android:onclick= "Begin"/>
< /linearlayout>

Java code implementation (simulated download progress update via thread):

 public class Progressbardemo extends appcompatactivity {@Override protected void Oncrea
Te (@Nullable Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.progressbar);} public void Begin (View v) {//Materialized Progress Bar dialog box (progressdialog) Final progressdialog pd = new ProgressDialog (this); Pd.settitle ("please
Slightly wait ");
Sets the dialog progress bar style to horizontal pd.setprogressstyle (progressdialog.style_horizontal);
Set hint information pd.setmessage ("in desperate downloading ...");
Set the dialog progress bar to appear at the top of the screen (for easy screenshots) Pd.getwindow (). setgravity (Gravity.top);
Pd.setmax (100); Pd.show ()//Call Show method to display progress Bar Dialog///use Anonymous inner class to implement thread and start new thread (new Runnable () {int initial = 0;//initial download Progress @Override public void R Un () {while (Initial<pd.getmax ()) {//Set loop condition Pd.setprogress (initial+=40);/set to complete every try {thread.sleep (1000); catch (Interruptedexception e)
{E.printstacktrace ();}
Pd.dismiss ()///The dialog disappears when progress completes}). Start (); }
}

The above is a small set of Android introduced by the implementation of the Thread Update ProgressDialog (Dialog progress bar), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.