0

I am trying to set the font of the POSITIVE_BUTTON in my AlertDialog to monospace but the font doesn't change. Here is the code:

    // Title text
    TextView settingsTitle =  new TextView(this);
    settingsTitle.setText("Settings");
    settingsTitle.setPadding(20, 20, 20, 20);
    settingsTitle.setTextSize(20);
    settingsTitle.setTypeface(Typeface.MONOSPACE);

    AlertDialog settingsDialog = new AlertDialog.Builder(this)
            .setCustomTitle(settingsTitle)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // continue with delete
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // do nothing
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();

    Typeface standardFont = Typeface.MONOSPACE;
    Button positiveButton = settingsDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    positiveButton.setTypeface(standardFont);
Henry Zhu
  • 2,488
  • 9
  • 43
  • 87
  • Have you tried calling `create()` instead of `show()` and calling `show()` after changing the typeface? – tbolender Jul 27 '15 at 19:24
  • The dialog doesn't know anything about your positive Button. To do this, you need a custom dialog layout. – Kristy Welsh Jul 27 '15 at 19:25
  • I think you have to use a Builder and a View to do this: http://stackoverflow.com/questions/13051956/how-to-set-custom-font-for-alert-dialog-in-android – Al Lelopath Jul 27 '15 at 19:25

1 Answers1

0

You need to create a custom dialog:

        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        Button dialogButton = (Button)dialog.findViewById(R.id.dialogButtonOK);
        Typeface standardFont = Typeface.MONOSPACE;
        dialogButton.setTypeface(standardFont)

        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

You can create your dialog layout to look like anything you want.

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106