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".
Asked
Active
Viewed 8,114 times
6
-
What kind of dialog are you using? Please show your code if you want a proper answer. – Codeman Jul 27 '15 at 21:46
-
@Pheonixblade9 Um... I said alert dialog if you read the question? – Henry Zhu Jul 27 '15 at 21:49
-
Yes, that is why I'm confused. You set the text yourself. There are no default options. – Codeman Jul 27 '15 at 21:50
-
this is a working solution : http://stackoverflow.com/questions/27893840/lollipop-capitalizes-buttons-text-in-my-app/39143743#39143743 – TharakaNirmana Aug 25 '16 at 11:13
5 Answers
15
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);
Rakesh
- 1,004
- 1
- 13
- 32
-
-
-
@KamrujjamanJoy Are you using `androidx.appcompat.app.AlertDialog`? If so, it should be. – David Lee Jul 06 '21 at 01:40
2
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
Gladwin Henald
- 61
- 1
- 6
1
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();
jesses.co.tt
- 2,628
- 1
- 29
- 48
-
No problem. When in doubt, double check the documentation - which can be done online or from your IDE! Also, posting the code you are trying to get work always helps us direct your/our efforts better! – jesses.co.tt Jul 27 '15 at 21:51
-
1this thing is not gonna work on lolipop and version greater than it. – satya prakash Aug 23 '17 at 09:21
-
-
Starting with Android 5.0, Buttons automatically have their text capitalized. you can't set capitaliztion in builder directly. – Burak Senel Feb 11 '22 at 10:08
0
using androidx.appcompat.app.AalertDialog fixed for me.
-
1Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 27 '22 at 06:48