I’ve set up a custom AlertDialog (actually, a custom TimePickerDialog). I’m using an OnShowListener, to update the buttons text according to a condition. I’ve put this code on a separate method:
private void changeState(){
if(!SleepAlarm.isActive()){
Log.d("RPod_MainActivity","SleepAlarm is NOT active");
this.setButton(TimePickerDialog.BUTTON1,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_enable), sleepEnable);
this.setButton(TimePickerDialog.BUTTON2,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_cancel), sleepDisable);
}
else{
Log.d("RPod_MainActivity","SleepAlarm is active");
this.setButton(TimePickerDialog.BUTTON1,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_change), sleepEnable);
this.setButton(TimePickerDialog.BUTTON2,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_disable), sleepDisable);
}
this.setTitle(R.string.sleep_title);
}
Once I’ve pressed the TimePickerDialog.BUTTON1 button, when I show the dialog again the condition is met, and the text should change. But it doesn’t.
I’ve checked with LogCat that the condition is ok. The first time I open the dialog I see “SleepAlarm is NOT active”. Once I’ve pressed the first button, when opening the dialog again I see “SleepAlarm is active”… But the buttons text are the same!
Here are the strings:
<string name="sleep_enable">Enable</string>
<string name="sleep_disable">Disable</string>
<string name="sleep_cancel">Cancel</string>
<string name="sleep_change">Change</string>
Edit: I’ve created the dialog using this code:
showDialog(TIME_DIALOG_ID);
The onCreateDialog implementation:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
// set time picker as current time
timePicker = new SleepPickerDialog(this,this);
return timePicker;
}
return null;
}
And SleepPickerDialog (extends TimePickerDialog) constructor:
public SleepPickerDialog(Context context, OnTimeSetListener listener){
super(context, listener, MainActivity.sleep_hour, MainActivity.sleep_hour, true);
changeState();
this.setOnShowListener(showListener);
}
Algo, the onShowListener:
private DialogInterface.OnShowListener showListener = new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
// Cambiar botones
changeState();
}
};
I am missing something important?
See accepted answer comments for the solution. I’m posting the complete code changes here:
public SleepPickerDialog(Context context, OnTimeSetListener listener){
super(context, listener, MainActivity.sleep_hour, MainActivity.sleep_hour, true);
this.setButton(BUTTON_POSITIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_enable), sleepEnable);
this.setButton(BUTTON_NEGATIVE,(CharSequence)MainActivity.currentContext.getResources().getText(R.string.sleep_cancel), sleepDisable);
this.setOnShowListener(showListener);
}
private void changeState(){
if(!SleepAlarm.isActive()){
this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_enable);
this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_cancel);
}
else{
this.getButton(BUTTON_POSITIVE).setText(R.string.sleep_change);
this.getButton(BUTTON_NEGATIVE).setText(R.string.sleep_disable);
}
this.setTitle(R.string.sleep_title);
}
see this link as per this it works
try