Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I toggle a progress dialog between determinate and indeterminate?

Tags:

android

I have an AsyncTask to handle a reasonably long running update process, and I want a progress dialog that shows, in order:

  1. "Starting update" (for a brief second)
  2. "Downloading update" (no progress bar — I don't know how big the update will be before I download it)
  3. "Saving update data" (with a 0-100% progress bar)
  4. "Saving update images" (with a 0-100% progress bar)
  5. "Update finished" (for a brief second before the dialog disappears)

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?

like image 500
George Avatar asked Nov 08 '12 04:11

George


People also ask

How do I make progress dialog not cancelable?

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.

Which dialog box is used to display the progress of the work?

Progress bars are used to show progress of a task.

Why is progress dialog deprecated?

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.


1 Answers

Might be a good idea to close one progress dialog and open another, with different style.

like image 162
lenik Avatar answered Nov 15 '22 18:11

lenik