How to use setProgressDrawable() correctly?
android, progress-bar
Solution
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!)
Problem
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?