Reputation: 33521
I have subclassed AlertDialog.Builder
to create my own customised dialog. In that dialog I added a CheckBox
. What I want is to change the text of the NegativeButton
from Cancel
to something else, when the CheckBox
is checked:
myCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// change text of negative button
}
}
});
How should I do that?
I have tried to get a reference to the button using
Button btnNegative = (Button)dlgView.findViewById(android.R.id.button1);
but that returns null
.
Upvotes: 0
Views: 123
Reputation: 33521
I was able to get an instance of the created AlertDialog
, and use its buttons like this:
class MyBuilder extends AlertDialog.Builder {
AlertDialog myDialog;
@Override
public AlertDialog show() {
myDialog = super.show();
return myDialog;
}
public MyBuilder(Activity act) {
CheckBox cb = ... ;
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Button btnNegative = myDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
}
});
}
}
Upvotes: 0
Reputation: 10083
UPDATE
Calling findViewById() will search for views within your Activity's layout and not your dialog's view. You need to call findViewById() on the specific View that you set as your dialog's layout.
View dialogView = inflater.inflate(R.layout.search_dialog, null);
searchDialog.setView(dialogView);
EditText tagText = (EdiText) dialogView.findViewById(R.id.tagField);
searchDialog.setPositiveButton( ... ) ...
AlertDialog myAlert = searchDialog.create(); //returns an AlertDialog from a Builder.
myAlert.show();
Try this may be useful read from a blog , may be useful
AlertDialog dialog = your_builder.create();
Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
Upvotes: 1