) Progressbar in Android

Source: Internet
Author: User
How do I display the progress bar and disable it when the task is not finished? Article The following is a simple example of displaying the entire progress bar after the experiment in other articles:

You can skip the steps for creating an android project and Google it.

The following describes the main activities.
Progressbardemo. JavaCode

  1. PackageCom. lveyo. Android. Demo. progressbar;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. App. progressdialog;
  4. ImportAndroid. OS. Bundle;
  5. ImportAndroid. OS. Handler;
  6. ImportAndroid. OS. message;
  7. ImportAndroid. View. view;
  8. ImportAndroid. widget. Button;
  9. ImportAndroid. widget. textview;
  10. Public ClassProgressbardemoExtendsActivity {
  11. PrivateTextview statustextview;
  12. PrivateButton beginbtn;
  13. PrivateProgressdialog;
  14. @ Override
  15. Public VoidOncreate (bundle savedinstancestate ){
  16. Super. Oncreate (savedinstancestate );
  17. Setcontentview (R. layout. Main );
  18. Statustextview = (textview) findviewbyid (R. Id. status );
  19. Beginbtn = (button) findviewbyid (R. Id. beginbtn );
  20. Setlistener ();
  21. }
  22. /** 
  23. * Use handler to update the UI 
  24. */
  25. PrivateHandler handler =NewHandler (){
  26. @ Override
  27. Public VoidHandlemessage (Message MSG ){
  28. // Close progressdialog
  29. Progressdialog. Dismiss ();
  30. // Update the UI
  31. Statustextview. settext ("Completed! ");
  32. }};
  33. /** 
  34. * Click the button event listener. 
  35. */
  36. Private VoidSetlistener (){
  37. Beginbtn. setonclicklistener (NewView. onclicklistener (){
  38. @ Override
  39. Public VoidOnclick (view v ){
  40. // Display progressdialog
  41. Progressdialog = progressdialog. Show (progressbardemo.This,"Loading ...","Please wait ...",True,False);
  42. // Create a thread
  43. NewThread (){
  44. @ Override
  45. Public VoidRun (){
  46. // Method that requires time Computing
  47. Calculation. Calculate (4);
  48. // Send a message to handler
  49. Handler. sendemptymessage (0);
  50. }. Start ();
  51. }
  52. });
  53. }
  54. }
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 progres Sdialog; @ 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 ();}/*** use handler to update UI */private handler = new handler () {@ overridepublic void handlemessage (Message MSG) {// disable progressdialogprogressdialog. Dismiss (); // update uistatustextview. settext ("completed! ") ;}};/*** Click the button event listener */private void setlistener () {beginbtn. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {// display progressdialogprogressdialog = progressdialog. show (progressbardemo. this, "loading... "," Please wait... ", true, false); // new thread () {@ overridepublic void run () {// The calculation method that takes time to calculate. calculate (4); // send a message to handler. sendemptymessage (0 );}}. start ();}});}}
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.