Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsyncTask with a ProgressDialog and Progress Bar

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.

like image 586
magneticMonster Avatar asked Jul 25 '11 17:07

magneticMonster


People also ask

What are the 3 generic types of Asynctask?

An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

Why is ProgressDialog deprecated?

ProgressDialog 's look can be replicated by placing a ProgressBar into an AlertDialog . You can still use it, but Android does not want you to use it, that is why it is deprecated.

Which method is used for progress bar?

You can update the percentage of progress displayed by using the setProgress(int) method, or by calling incrementProgressBy(int) to increase the current progress completed by a specified amount. By default, the progress bar is full when the progress value reaches 100.


2 Answers

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

like image 143
Nikola Despotoski Avatar answered Sep 25 '22 13:09

Nikola Despotoski


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();
like image 27
salman khalid Avatar answered Sep 25 '22 13:09

salman khalid