show and dismiss progress dialog while running a thread in android
15,358
Try this .. it's simple
ProgressDialog progress = new ProgressDialog(this);
progress.Indeterminate = true;
progress.SetProgressStyle(ProgressDialogStyle.Spinner);
progress.SetMessage("Downloading Files...");
progress.SetCancelable(false);
RunOnUiThread(() =>
{
progress.Show();
});
Task.Run(()=>
//downloading code here...
).ContinueWith(Result=>RunOnUiThread(()=>progress.Hide()));
Author by
Anju
Updated on June 29, 2022Comments
-
Anju almost 2 years
I have a method in my activity to download a set of files. This downloading is taking place when I start a new activity. I have used threads, because it downloads completely whereas AsyncTask may sometimes fail to download all files, it may get stuck in between.
Now, a black screen is shown when the downloading takes place. I want to show it within a ProgressDialog so that user may feel that something is getting downloaded.
I have added a ProgressDialog, but its not showing. Can anyone tell where did I go wrong?
Below is my code:
Inside
onCreate()
I have written:downloadFiles(); private boolean downloadFiles() { showProgressDialog(); for(int i = 0; i < filesList.size();i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { //downloading code }); thread.start(); thread.run(); } dismissProgressDialog(); return true; } //ProgressDialog progressDialog; I have declared earlier. private void showProgressDialog() { progressDialog = new ProgressDialog(N12ReadScreenActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setMessage("Downloading files..."); progressDialog.show(); } private void dismissProgressDialog() { if(progressDialog != null) progressDialog.dismiss(); }