Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
}

}

like image 303
Zach Avatar asked Nov 03 '15 01:11

Zach


People also ask

What can I use instead of progress dialog?

"Deprecated" refers to functions or elements that are in the process of being replaced by newer ones. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI.

Which method is used to update the progress dialog with some specific value?

setProgress(int value) This method is used to update the progress dialog with some specific value.

What is progress dialog in Android?

Android Progress Dialog is a UI which shows the progress of a task like you want user to wait until the previous lined up task is completed and for that purpose you can use progress dialog. The best example is you see when downloading or uploading any file.


1 Answers

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.

like image 57
Aaron Avatar answered Sep 28 '22 15:09

Aaron