'Android how to make ok button on dialog not all caps

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



Solution 1:[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)

Solution 2:[2]

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

Solution 3:[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

Solution 4:[4]

Or even :

builder.setPositiveButton("Ok", null);

For the basic usage.

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jesses.co.tt
Solution 2 Rakesh
Solution 3 Gladwin Henald
Solution 4 Baakan
Solution 5 Mandar Joshi