16

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?

user3098538
  • 1,201
  • 3
  • 13
  • 15

3 Answers3

21

Instead of setting the button text to be "add", set it to be Html.fromHtml("<b><i>add</i></b>")

So for with your code:

Change these lines:

builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);

To these lines:

builder.setPositiveButton(Html.fromHtml("<b><i>" + getString(R.string.ok_button) + "</i><b>"), null);
// and 
builder.setNegativeButton(Html.fromHtml("<b><i>" + getString(R.string.cancel_button) + "</i><b>"), null);

OR

you could modify the Strings in your strings.xml file.

So for example, if your strings looked like this:

<string name="ok_button">add</string>
<string name="cancel_button">cancel</string>

you could change them to this:

<string name="ok_button"><b><i>add</i></b></string>
<string name="cancel_button"><b><i>cancel</i></b></string>

but

you still are referencing your Strings resources incorrectly. Instead of R.string.ok_button, because that returns an int, you would have to use getString(R.string.ok_button)

So you would have to change these lines:

builder.setPositiveButton(R.string.ok_button, null);
// and
builder.setNegativeButton(R.string.cancel_button, null);

To these lines:

builder.setPositiveButton(getString(R.string.ok_button), null);
// and
builder.setNegativeButton(getString(R.string.cancel_button), null);
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • I work fine for me, but when I change the English text to Korean text it does not show in Italic. – user3098538 Dec 27 '13 at 04:08
  • @user3098538 I'm not sure why that would be. Try both methods in my answer and if neither of them work for Korean text, post it as another question and someone else could answer it for you. – Michael Yaworski Dec 27 '13 at 04:20
  • I tried already but it is still the same. Thank for your advise. – user3098538 Dec 27 '13 at 04:23
  • Using an integer for the button text is not incorrect, that's a perfectly valid thing to do (pass a string resource) – Greg Ennis Sep 08 '16 at 17:49
21

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();
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
8

You can access your Dialog's buttons within onShow() like I mentioned in below. At time of onShow() triggering you are able to access yoir dialog's UI, try this,

private void showAlertDialog(){

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    builder.setMessage("Dailog Demo!");

    builder .setCancelable(false)
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           dialog.cancel();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
           dialog.cancel();
       }
   });

    final AlertDialog alertdialog = builder.create();
    alertdialog.setOnShowListener(new OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            ((Button)alertdialog.getButton(Dialog.BUTTON_POSITIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
            ((Button)alertdialog.getButton(Dialog.BUTTON_NEGATIVE)).setTypeface(null, Typeface.BOLD_ITALIC);
        }
    });
    alertdialog.show();
}
SathishKumar
  • 1,644
  • 1
  • 14
  • 28