简体   繁体   中英

How to change a progress dialog layout when the progress completed?

I have a progress dialog in my AsyncTask extended class. I have set the layout to the progress dialog using setcontentview(). I want to change the design of the progress dialog and show for 2 seconds when it is complete (ie in PostExecute()).

I start Progress dialog in onPreexecute() and dismiss it in onPostexecute() methods.

This is how I initialised progress dialog in onPreexecute

progressDialog = ProgressDialog.show(context, null, null);
progressDialog.setContentView(R.layout.progress_meditate);
progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
progressDialog.setProgressDrawable(new ColorDrawable(Color.WHITE));

I tried below code in onPostExecute

progressDialog.setContentView(R.layout.progress_complete);
sleep(2000);
progressDialog.dismiss();

but it is not working. My progress_complete layout is not getting shown. Progress dialog gets dismissed after sleep time with same design.

01. First step initialize the progressBar and ProgressBarText

 private ProgressBar progressBar;
 private TextView progressBarText;

02. The second step declare this OnCreate Method

  progressBar.setVisibility(View.VISIBLE);
  progressBarText.setVisibility(View.VISIBLE);

03. The third step declare this onPostExecute Method

  progressBar.setVisibility(View.GONE);
  progressBarText.setVisibility(View.GONE);

That's it. I think it's is help for you.

For this you can not change the design on runtime. the only possible way of showing a dialog with different design on post execute is that create a new dialog and dismiss the previous one and show the new one.

The less simple solution would be to extend or creating your own ProgressDialog. On the other hand in the Android documentation you can find the following :

Caution : Android includes another dialog class called ProgressDialog that shows a dialog with a progress bar. This widget is deprecated because it prevents users from interacting with the app while progress is being displayed. If you need to indicate loading or indeterminate progress, you should follow the design guidelines for Progress & Activity and use a ProgressBar in your layout, instead of using ProgressDialog.

I Would suggest/recommend to shift to ProgressBar as the starting point of the solution but it is your call.

I think it would be more easy to solve your issue because ProgressBar is just another view that can be overlap on finish.

Then create two progress dialog, one above another, dismiss one first and another one after 2 seconds. It will work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM