6

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,168
  • 7
  • 36
  • 83

5 Answers5

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
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
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,628
  • 1
  • 29
  • 48
0

Or even :

builder.setPositiveButton("Ok", null);

For the basic usage.

Baakan
  • 89
  • 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