Set AlertDialog Positive Button Text to be Bold

user3098538 picture user3098538 · Dec 26, 2013 · Viewed 11.5k times · Source

Here is my code.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View vi = inflater.inflate(R.layout.group_dialog_layout,null);
    builder.setView(vi);
    TextView txtNewGroupEntry = (TextView) vi.findViewById(R.id.txtGroupRename);
    if(isNew==true){
        builder.setTitle("New Group");
        txtNewGroupEntry.setText(R.string.new_group_instruction);
    }
    builder.setPositiveButton(R.string.ok_button, null);
    builder.setNegativeButton(R.string.cancel_button, null);
    AlertDialog dialog = builder.create();
    dialog.show();
    Button okButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);

I have an alert dialog with an add button and a cancel button. I want both of the button's text to be bold and italic. How can I do it?

Answer

Gopal Gopi picture Gopal Gopi · Dec 26, 2013

this sample code will help you...

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("message").setTitle("title");
    AlertDialog alertDialog = builder.create();
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
                // do work
                break;
            case DialogInterface.BUTTON_NEUTRAL:
                // do work
                break;
            case DialogInterface.BUTTON_NEGATIVE:
                // do work
                break;
            default:
                break;
            }
        }
    };
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", listener);
    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", listener);
    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "Cancel",
            listener);
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            AlertDialog alertDialog = (AlertDialog) dialog;
            Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);

            button = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);

            button = alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL);
            button.setTypeface(Typeface.DEFAULT, Typeface.BOLD | Typeface.ITALIC);
        }
    });
    alertDialog.show();