Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to make ok button on dialog not all caps

Tags:

java

android

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

like image 538
Henry Zhu Avatar asked Jul 27 '15 21:07

Henry Zhu


3 Answers

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);
like image 124
Rakesh Avatar answered Oct 01 '22 01:10

Rakesh


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
like image 23
Gladwin Henald Avatar answered Oct 01 '22 01:10

Gladwin Henald


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)

like image 26
jesses.co.tt Avatar answered Oct 01 '22 03:10

jesses.co.tt