Android modifies ProgressDialog when dialog is running

I have a ProgressDialog that I started when I created an Activity (inside the inOnCreate method).

private void initDialog() { mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setTitle("Please wait."); mProgressDialog.setMessage("Connecting to LinkedIn."); mProgressDialog.setCancelable(false); mProgressDialog.show(); } 

After 2500 ms, I want to change the circle animation to my custom image (see below) (for example, a simulation of when I finish receiving some data from the server, and I finished.) So, I need to show the user that the process is complete. For this purpose, I choose the following path.

  • I am showing a dialogue
  • When I retrieve the data, I change the drawable of ProgressDialog

And I have a problem. When I install the first time

 private void initDialog() { mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setTitle("Please wait."); mProgressDialog.setMessage("Connecting to LinkedIn."); mProgressDialog.setCancelable(false); //This mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok)); mProgressDialog.show(); } 

ProgressDialog changes and everything looks fine. But when the ProgressBar starts up (circle animation works / works), I try to reset to draw

 mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok)); 

And I have an unexpected result: the circle animation disappears, and the image is not set to the current progress. NO PICTURES! BLANK AREA instead of my image.

  authButton.postDelayed(new Runnable() { @Override public void run() { mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ok)); //doesn't work as expected! } }, 2500); 

So my question is: how can I change the availability at runtime while the ProgressDialog is running?

enter image description here need to change to enter image description here

+5
source share
2 answers

Finally, I will find a solution

ProgressDialog contains inside a ProgressBar that displays our capabilities.

 View view = inflater.inflate(a.getResourceId(com.android.internal.R.styleable.AlertDialog_horizontalProgressLayout, R.layout.alert_dialog_progress), null); mProgress = (ProgressBar) view.findViewById(R.id.progress); // THIS mProgressNumber = (TextView) view.findViewById(R.id.progress_number); mProgressPercent = (TextView) view.findViewById(R.id.progress_percent); setView(view); 

When we try again to set drawable to ProgressDialog again, ProgressDialog will really install a new drawable . But this drawable has no boundaries (coordinates for displaying this view). So we have to fix this with difficulty. I choose the copy method - just the copy limit from the current value of ProgressDialog . You can use another way.

Initialization Dialog:

 private void initDialog() { mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setTitle("Please wait."); mProgressDialog.setMessage("Connecting to LinkedIn."); mProgressDialog.setCancelable(false); } 

Show

  mProgressDialog.show(); 

Change the stretchable

 private void changeToDone(int resId) { //Getting a progressBar from dialog ProgressBar bar = (ProgressBar) mProgressDialog.findViewById(android.R.id.progress); //Getting a DONE(new) drawable from resources Drawable drawable = getResources().getDrawable(resId); //Getting a drawable from progress dialog Drawable indeterminateDrawable = bar.getIndeterminateDrawable(); //Obtain a bounds of current drawable Rect bounds = indeterminateDrawable.getBounds(); //Set bounds to DONE(new) drawable drawable.setBounds(bounds); //Set a new drawable bar.setIndeterminateDrawable(drawable); mProgressDialog.setTitle("Done."); mProgressDialog.setMessage("Connected."); } 

Note:

The decision has not been verified in specific cases, such as

  • We have a progress bar in ActionBar / TitleBar
  • Other cases where we already have android.R.progress in the screen window. In this case, I think my decision will lead to unexpected behavior.
+5
source
 private void initDialog(boolean is) { mProgressDialog = new ProgressDialog(this); mProgressDialog.setIndeterminate(true); mProgressDialog.setTitle("Please wait."); mProgressDialog.setMessage("Connecting to LinkedIn."); mProgressDialog.setCancelable(false); if(is) // take global variable mProgressDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.ic_launcher)); mProgressDialog.show(); if(!is) new Handler().postDelayed(new Runnable() { @Override public void run() { mProgressDialog.cancel(); mProgressDialog = null; initDialog(true); } }, 2500); 

To call for the first time using initDialog(false);

As far as I can check for an indefinite drawable, it cannot really be replaced, so this workaround can save you from creating a custom ProgressBar.

+2
source

Source: https://habr.com/ru/post/1215772/


All Articles