title: How do I toggle a progress dialog between determinate and indeterminate?
body: <p>I have an AsyncTask to handle a reasonably long running update process, and I want a progress dialog that shows, in order:</p>
  
  <ol>
  <li>"Starting update" (for a brief second)</li>
  <li>"Downloading update" (no progress bar &mdash; I don't know how big the update will be before I download it)</li>
  <li>"Saving update data" (with a 0-100% progress bar)</li>
  <li>"Saving update images" (with a 0-100% progress bar)</li>
  <li>"Update finished" (for a brief second before the dialog disappears)</li>
  </ol>
  
  <p>I'm having problems toggling the progress dialog between determinate (progress bar shows up) and indeterminate (no progress bar).</p>
  
  <p>The code in my AsyncTask follows:</p>
  
  <pre><code>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 &gt; 0) {
          progressDialog.setIndeterminate(false);
          progressDialog.setMax(progressTotal);
          progressDialog.setProgress(progressProgress);
      } else {
          progressDialog.setIndeterminate(true);
          progressDialog.setMax(0);
          progressDialog.setProgress(0);
      }
  }
  </code></pre>
  
  <p>I tried toggling determinate with <code>setIndeterminate()</code> &mdash; 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.</p>
  
  <p>Then I tried using <code>setProgressNumberFormat()</code> and <code>setProgressPercentFormat()</code> just to hide the numbers &mdash; but neither of these are supported in Android below 3.0.</p>
  
  <p>Then I tried using <code>setProgressStyle()</code> to toggle between STYLE_SPINNER and STYLE_HORIZONTAL &mdash; calling <code>setProgressStyle()</code> in my <code>onProgressUpdate()</code> seems to cause my app to crash.</p>
  
  <p>Is there any easy way to toggle a progress dialog between determinate and indeterminate mode?</p>