Is it possible to change PD’s style, message and title on the fly (and not from UI thread)?
I want to do the following. Fist time PD shows in STYLE_SPINNER as it looks like endless progress and shows the message informing user that app is looking 4 something (some update 4 instance). And when it found that something it has to do something with it (install it). At this point i wanna show the HORIZONTAL styled PD as it really shows the progress state instead of spinner styled one.
Anyway I got NullPointerException while trying to PD.setMax(). PD isn’t null so I can’t get what’s going on.
This is an inner class in my Activity class:
private class RestoreDBTask extends AsyncTask <Void, Void, String>
{
private ProgressDialog dialog;
private Handler handler;
@Override
protected void onPreExecute()
{
this.dialog = new ProgressDialog(SplashActivity.this);
this.dialog.setTitle(getString(R.string.progress_wait));
this.dialog.requestWindowFeature(Window.FEATURE_PROGRESS);
// this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
this.dialog.setProgress(0);
this.dialog.show();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
RestoreDBTask.this.dialog.hide();
switch (msg.what) {
case 0:{
RestoreDBTask.this.dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
break; }
case 1: {
RestoreDBTask.this.dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
break; }
case -1: {
RestoreDBTask.this.dialog.setMessage(getResources().getString(R.string.progress_db_installing));
break; }
case -2: {
RestoreDBTask.this.dialog.incrementProgressBy(1);
break; }
default: {
Log.i(APP_TAG,""+RestoreDBTask.this.dialog.getMax());
RestoreDBTask.this.dialog.setMax(msg.what); }
}
RestoreDBTask.this.dialog.show();
}
};
}
@Override
protected String doInBackground(Void... params)
{
mDBHelper.initDB(dialog,handler);
return "";
}
@Override
protected void onPostExecute(String result)
{
dialog.dismiss();
startNextActivity();
}
}
the mDBHelper.initDB() method uses handler.sendEmptyMessage() calls with some int values assuming if msg.what>1 it is Max value for PD.
Also I can’t figure out why PD doesn’t show the progress state if it’s style is STYLE_SPINNER? If i change it to STYLE_HORIZONTAL it works ok…
It’s not possible to update UI stuff in another
Thread. What you should do is to overrideonProgressUpdate()and there you can update the UI while the code indoInBackground()still processing.Sample code: