For an alert dialog in Android, how do you make the positive button not have all capital letters. The text is "OK" instead of "Ok".
The accepted solution above won't work in Lollipop and above. Here's the working solution.
After showing the dialog, I'm setting the button all caps false. Make sure you do it after dialog.show()
. Else, you'll get Null Pointer Exception
.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do Something
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);
Use the DialogInterface.BUTTON_POSITIVE or DialogInterface.BUTTON_NEGATIVE to customize the action buttons.
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.alert_title))
builder.setMessage(getString(R.string.alert_msg))
builder.setPositiveButton(getString(R.string.action_yes)) { _, _ ->
// todo: your action
}
builder.setNegativeButton(getString(R.string.action_no), null)
val dialog = builder.create()
dialog.show()
dialog.getButton(DialogInterface.BUTTON_POSITIVE).isAllCaps = false
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).isAllCaps = false
You can set it to be anything you want - Eg.:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog dialog = builder.create();
dialog.show();
Reference: http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(int, android.content.DialogInterface.OnClickListener)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With