I am having problem with setting a new Drawable to my ProgressBar.
If I use the setProgressDrawable() inside onCreate() method it works great. But when I try to call the same method inside a Handler post callback it doesn't work and the progressbar disapears.
Can someone explain this behaviour? How can I solve this problem?
Bumped into this problem myself and I managed to get it working :)
I used the AsyncTask
to handle the background tasks/threads, but the idea should be the same as using Runnable/Handler
(though AsyncTask
does feel nicer imo).
So, this is what I did... put setContentView(R.layout.my_screen);
in the onPostExecute
method! (ie. instead of the onCreate
method)
So the code looks something like this:
public class MyScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.my_screen); !!! Don't setContentView here... (see bottom)
new MySpecialTask().execute();
}
private int somethingThatTakesALongTime() {
int result;
// blah blah blah
return result;
}
private void updateTheUiWithResult(int result) {
// Some code that changes the UI
// For exampe:
TextView myTextView = (TextView) findViewById(R.id.result_text);
myTextView.setText("Result is: " + result);
ProgressBar anyProgressBar = (ProgressBar) findViewById(R.id.custom_progressbar);
anyProgressBar.setProgressDrawable(res.getDrawable(R.drawable.progressbar_style));
anyProgressBar.setMax(100);
anyProgressBar.setProgress(result);
}
private class MySpecialTask extends AsyncTask<String, Void, Integer> {
ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(MyScreen.this, "", "Calculating...\nPlease wait...", true);
}
@Override
protected Integer doInBackground(String... strings) {
return somethingThatTakesALongTime();
}
@Override
protected void onPostExecute(Integer result) {
mProgressDialog.dismiss();
setContentView(R.layout.my_screen); // setContent view here... then it works...
updateTheUiWithResult(result);
}
}
}
To be honest, why you need to call setContentView
in onPostExecute
I have no idea... but doing so means you can set custom styles for your progress bars (and they don't disappear on you!)
downloadingBar.setProgress(0);
Drawable progressDrawable = getResources().getDrawable(R.drawable.download_progressbar_pause_bg);
progressDrawable.setBounds(downloadingBar.getProgressDrawable().getBounds());
downloadingBar.setProgressDrawable(progressDrawable);
downloadingBar.setProgress(mCurrentPercent);
- First you should reset the progress to zero
- Set the progress drawable bounds
- Set new progress drawable
- Set new progress
Maybe you put the code in a thread which is not main thread.
If you want to work with the UI, you must do that in the main thread :)
I was also facing the same issue but in my case it is due to the use of Drawable.mutate() method. When i removed that method it started working fine. I also noticed that this issue exist below api level-21(lollipop).