20

I am attempting to use AsyncTask to load a file of determinate length. My AsyncTask looks something like this:

protected void onPreExecute() {
    dialog = ProgressDialog.show(MyActivity.this, null, "Loading", false);
}


protected void onProgressUpdate(Integer... values) {
    if (values.length == 2) {
        dialog.setProgress(values[0]);
        dialog.setMax(values[1]);
    }
}

in my doInBackground() implementation I call publishProgress(bytesSoFar, maxBytes); inside my loading loop and in the onPostExecute() I call dialog.dismiss().

However, I can't get the ProgressDialog to show anything but an indeterminate spinner. I want to see a horizontal progress bar that shows the progress as the loading happens. I've debugged and can see that onProgressUpdate() gets called with sane values and that the dialog's methods are getting called.

magneticMonster
  • 2,373
  • 6
  • 30
  • 46
  • possible duplicate of [Progressbar togther with asyncTask](http://stackoverflow.com/questions/4119009/progressbar-togther-with-asynctask) – Siddharth Lele Jan 23 '13 at 13:14

2 Answers2

29

Add Style to your progress dialog with before you show it .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 1
    It looks like I was calling setStyle() before rather than setProgressStyle(). Also, I was under the impression that the ProgressDialog.show() method with a boolean indeterminate flag would do that work for you (otherwise what's the point?). Guess I was wrong! – magneticMonster Jul 25 '11 at 18:16
9

Use this code in your onPreExecute().

 ProgressDialog prog;
 prog = new ProgressDialog(ctx);
 prog.setTitle(title);
 prog.setMessage(msg);       
 prog.setIndeterminate(false);
 prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 prog.show();
dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
salman khalid
  • 4,884
  • 4
  • 27
  • 33