Progress Dialog Shows but no Progress Bar Shows and Message Isn't Updating
Asked Answered
C

2

8

I have this class that gets called from a fragment. On progress update gets called but the message will not update. I also am not seeing any progress bar or spinner. Just the title and the message, seen some similar problems but nothing where the progress bar isn't showing at all. Also, my message will not update at all in onProgressUpdate but printing out the values does show that it increments inside of the onProgressUpdate.

Edit: Here is how I start the task

DownloadFilesTask download = new DownloadFilesTask();
download.execute(urls.toArray(new String[urls.size()]));

Here is the class

private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {

ProgressDialog progressDialog;

@Override
protected void onPreExecute()
{
    progressDialog = ProgressDialog.show(getActivity(), "Downloading","Downloaded 0/"+urls.size(), false);
    progressDialog.setProgress(0);
    progressDialog.setMax(urls.size());
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}

@Override
protected Long doInBackground(String[] urls) {
    int count = urls.length;
    long totalSize = 0;
    for (int i = 0; i < count; i++) {
            //Do things in background here
            publishProgress(new Integer[] {i});
    }
    return totalSize;
}

@Override
protected void onProgressUpdate(final Integer... progress) {
    System.out.println(progress[0]); //This does print correctly
    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            progressDialog.setProgress(progress[0]);
            progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size());
        }
    });
}

@Override
protected void onPostExecute(Long result) {
    progressDialog.dismiss();
    Toast t = Toast.makeText(getActivity(), "Downloaded", Toast.LENGTH_LONG);
    t.show();
}

}

Criticaster answered 3/11, 2015 at 1:12 Comment(2)
How did you start the task,please show the code.Josefajosefina
Edited it to show the way I start it, its just the generic way you start a task so that is why I didn't include it. Does that help at all?Criticaster
A
5

The issue is with the way your ProgressDialog is initialized.

Replace your onPreExecute with this:

@Override
protected void onPreExecute() {
    progressDialog = new ProgressDialog(activity);
    progressDialog.setTitle("Downloading");
    progressDialog.setMessage("Downloaded 0/" + urls.size());
    progressDialog.setIndeterminate(false);
    progressDialog.setProgress(0);
    progressDialog.setMax(urls.size());
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.show();
}

Essentially, you need to call setProgressStyle before calling show.

Additionally, you can remove the runOnUiThread code in onProgressUpdate because onProgressUpdate is invoked on the UI thread.

Abomasum answered 24/12, 2015 at 0:36 Comment(2)
Wow, such a simple issue. Thank you very much, will award bounty when I can ~22 hrs leftCriticaster
Glad to help - it is a tricky bug. If you're interested, take a look at the ProgressDialog source - you'll see why you were running into this issue. github.com/android/platform_frameworks_base/blob/master/core/…Abomasum
C
0

Not sure of the exact cause, but it is not required to setProgress on runnable as onProgressUpdate will run on UI thread.

Try Replacing

getActivity().runOnUiThread(new Runnable() { @Override public void run() { progressDialog.setProgress(progress[0]); progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size()); } });

with

progressDialog.setProgress(progress[0]); progressDialog.setMessage("Downloaded "+ progress[0] +"/"+urls.size());

Also - Are you doing networking stuff in doInbackground(), else we cannot see progress which will be dismissed on onPostExecute().

Carcinoma answered 3/11, 2015 at 1:29 Comment(2)
I actually had that before but it wasn't working so I decided to try the runonuithread. I am downloading images so yes it does take a while. The dialog shows but there is no progress bar in it, just the title. Once it is finished downloading the images it closes just fineCriticaster
Ok. May be try initialising the progress dialog first and call the show() later at the end of onPreExecute()Carcinoma

© 2022 - 2024 — McMap. All rights reserved.