I have an AsyncTask to handle a reasonably long running update process, and I want a progress dialog that shows, in order:
I'm having problems toggling the progress dialog between determinate (progress bar shows up) and indeterminate (no progress bar).
The code in my AsyncTask follows:
private final ProgressDialog progressDialog;
public SynchronizeTask(Activity activity)
{
progressDialog = new ProgressDialog(activity);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
protected void onPreExecute() {
progressDialog.setMessage("Starting update...");
progressDialog.setCancelable(false);
progressDialog.show();
}
...lots of code here...
// all of these are set from doInBackground()
private String progressMsg;
private int progressTotal;
private int progressProgress;
protected void onProgressUpdate(Void... values) {
progressDialog.setMessage(progressMsg);
if (progressTotal > 0) {
progressDialog.setIndeterminate(false);
progressDialog.setMax(progressTotal);
progressDialog.setProgress(progressProgress);
} else {
progressDialog.setIndeterminate(true);
progressDialog.setMax(0);
progressDialog.setProgress(0);
}
}
I tried toggling determinate with setIndeterminate()
— the problem here is that the progress dialog shows "NaN" and "0%" at the bottom of the dialog even though there's no "bar" left in indeterminate mode.
Then I tried using setProgressNumberFormat()
and setProgressPercentFormat()
just to hide the numbers — but neither of these are supported in Android below 3.0.
Then I tried using setProgressStyle()
to toggle between STYLE_SPINNER and STYLE_HORIZONTAL — calling setProgressStyle()
in my onProgressUpdate()
seems to cause my app to crash.
Is there any easy way to toggle a progress dialog between determinate and indeterminate mode?
So how can I make it so that the Dialog is not cancelable? Android seems to be saying that ProgressDialog should not be used anymore, that instead you should “use a ProgressBar in your layout.” developer.android.com/guide/topics/ui/dialogs.html.
Progress bars are used to show progress of a task.
A ProgressDialog would hold a ProgressBar inside an Alert Dialog. ProgressDialog is now deprecated since it isn't a good idea to show long progress in a dialog while blocking the screen.
Might be a good idea to close one progress dialog and open another, with different style.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With