9

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".

Henry Zhu
  • 2,488
  • 9
  • 43
  • 87

6 Answers6

22

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,205
  • 1
  • 14
  • 33
3

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
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();

Reference: http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(int, android.content.DialogInterface.OnClickListener)

jesses.co.tt
  • 2,689
  • 1
  • 30
  • 49
0

Or even :

builder.setPositiveButton("Ok", null);

For the basic usage.

Baakan
  • 99
  • 1
  • 10
0

using androidx.appcompat.app.AalertDialog fixed for me.

  • 1
    Your 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
0

its late, but it could help other who visits.

To change the button text case, from all capitals to small case, usual method such as

dialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);

this will not work with the alert builder anymore, upon exploring alert dialog code, I found Buttons used for it, and as default it will show capital text.

To set, we need to add style while creating the alert dialog. Follow below steps for the same.

  1. In your theme.xml or style.xml add below style.
<style name="custom_alert_button_text" parent="ThemeOverlay.MaterialComponents.Dialog.Alert">
        <item name="android:textAllCaps">false</item>
        <item name="android:textSize">@dimen/_13ssp</item>
        <item name="android:textAppearance">?textAppearanceFiraSansBoldBlue34Text</item>
</style>
  1. While building the alert dialog, add style to your alert dialog as follow.
val alertBox = AlertDialog.Builder(context, R.style.custom_alert_button_text)
        alertBox.setCancelable(false)
        alertBox.setMessage("Your message for dialog")
        alertBox.setNegativeButton(
            "No"
        ) { dialog, _ ->
            dialog.dismiss()
        }
        alertBox.setPositiveButton(
            "Yes"
        ) { dialog, _ ->
            dialog.dismiss()
            //your action to perform on yes tap.
        }
        alertBox.show()

This way, the positive and negative buttons text cases can be changed.

I am using this method to change the text cases for alert dialog with builder.

Hope it helps!