Progress bar activity is not starting when clicking on the button in android -


i have written following code inside method called on clicking on button:

final progressdialog progressdialog = new progressdialog(this);         progressdialog.setprogress(0);           progressdialog.seticon(r.drawable.ic_launcher);         progressdialog.settitle("downloading files…");         progressdialog.setprogressstyle(progressdialog.style_horizontal);         progressdialog.setbutton(dialoginterface.button_positive,"ok",             new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog,                 int whichbutton)                 {                     toast.maketext(getbasecontext(),                             "ok clicked!", toast.length_short).show();                 }         });         progressdialog.setbutton(dialoginterface.button_negative, "cancel",             new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog,                     int whichbutton)                 {                     toast.maketext(getbasecontext(),                             "cancel clicked!", toast.length_short).show();                 }         });         new thread(new runnable(){             public void run(){                 (int i=1; i<=20; i++) {                     try {                         //---simulate doing lengthy---                         thread.sleep(1000);                         //---update dialog---                         progressdialog.incrementprogressby((int)(100/20));                     } catch (interruptedexception e) {                                            e.printstacktrace();                     }                 }                 progressdialog.dismiss();             }         }).start();     } 

when click on button, progress bar doesn't start.

two things

  1. you need call show on progressdialog instance otherwise never shown
  2. progressdialog.incrementprogressby((int)(100/20)); has run on ui thread otherwise calledfromwrongtreadexception

to overcome point 2 can use handler or runonuithread


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -