I am downloading multiple files in an AsyncTask with the help of for() loop. Below code works fine but each file downloaded with its own and single progress bar, I want only one
I think you have 2 options:
The fake progress bar approach
You know in advance how many files you need to download, you can set the ProgressDialog
total amount to the number of files to download. This works pretty well with files which are small and similar in dimensions and gives the user a good feedback about what's going on.
// you can modify the max value of a ProgressDialog, we modify it
// to prevent unnecessary rounding math.
// In the configuration set the max value of the ProgressDialog to an int with
pDialog.setMax(urls.length);
for (int i = 0; i < urls.length; i++) {
// launch HTTP request and save the file
//...
// your code
//...
//advance one step each completed download
publishProgress();
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(Integer... progress) {
pDialog.incrementProgressBy(1);
}
The real progress bar approach
You need to know in advance the total length of all the files you need to download. For example, you could create a separate REST API to call before everything else which give you the total length in bytes before you start to download each separate files. In this way, you can periodically update the total ProgressDialog
length accordingly to the total bytes you have already downloaded.