Note that there are some explanatory texts on larger screens.

plurals
  1. POProgressDialog in Android - Indicator shows full even for 30%
    primarykey
    data
    text
    <p>I'm trying to create a Progress Dialog as shown below in the code. But the problem is that when I hit cancel, and click on the progress bar(Edit:Display progress bar button) again, it doesn't work as expected. The indicator moves faster, and some times quits without even completing.</p> <p>This is my code to call onClick3 when you click 'Click to Display...' button on the screen shot. </p> <pre><code>ProgressDialog progressDialogAdvanced; public void onClick3(View v ){ showDialog(1); progressDialogAdvanced.setProgress(0); t = new Thread(new Runnable() { @Override public void run() { for(int i = 1; i &lt;=10; i++){ try{ Thread.sleep(500); progressDialogAdvanced.incrementProgressBy((int)10); }catch(InterruptedException e ){ e.printStackTrace(); } } progressDialogAdvanced.dismiss(); } }); t.start(); } </code></pre> <p>This is the code to create the progressbar:</p> <pre><code>protected Dialog onCreateDialog(int id) { switch (id) { case 1: progressDialogAdvanced = new ProgressDialog(this); progressDialogAdvanced.setIcon(R.drawable.ic_launcher); progressDialogAdvanced.setTitle("Downloading..."); progressDialogAdvanced.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialogAdvanced.setButton(DialogInterface.BUTTON_POSITIVE,"OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //To change body of implemented methods use File | Settings | File Templates. //progressDialogAdvanced.setProgress(0); Toast.makeText(getBaseContext(),"OkClicked",Toast.LENGTH_SHORT).show(); } }); progressDialogAdvanced.setButton(DialogInterface.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { progressDialogAdvanced.dismiss(); Toast.makeText(getBaseContext(),"Cancelled",Toast.LENGTH_SHORT).show(); } }); return progressDialogAdvanced; } } </code></pre> <p><img src="https://i.stack.imgur.com/jlR3V.png" alt="Indicator showing 100% right from 0 to 100"></p> <p>Edit:</p> <p>Used cancel() instead of dismiss(). Used Handler as shown below Still doesn't fix anything. I have to wait till the thread gets destroyed.</p> <pre><code>for(int i = 1; i &lt;=10; i++){ try{ Thread.sleep(500); }catch(InterruptedException e ){ e.printStackTrace(); } progressBarHandler.post(new Runnable() { public void run() { progressDialogAdvanced.incrementProgressBy(10); } }); } progressDialogAdvanced.cancel(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload