3

I am showing an AlertDialog on a button click which will have positive and negative buttons which are the default buttons of AlertDialog with a message.

I want to change the font family of those buttons & message, how can I change it?

Below is my code:

AlertDialog.Builder altDialog = new AlertDialog.Builder(context);
altDialog.setMessage(Constants.WARNING_STEPS);
altDialog.setTitle(Constants.WARNING);
altDialog.setCancelable(true);

altDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  @Override
    public void onClick(DialogInterface dialog, int which) {

});

altDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});

altDialog.show();
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Pankaj
  • 7,908
  • 6
  • 42
  • 65

3 Answers3

2

The key point here is the address of the textviews and buttons in android.R.id file which is android.R.id.button1 and android.R.id.button2 and android.R.id.button3 and also android.R.id.message

       AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)                                                                                                         
                    .setMessage("آیا مایل به خروج از حساب کاربری هستید؟")
                    .setCancelable(true)
                    .setPositiveButton("بله", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                          ...

                        }

                    })
                    .setNegativeButton("خیر", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                           ...
                        }

                    }).show();

            TextView textView = (TextView) dialog.findViewById(android.R.id.message);
            textView.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));

            Button btn1 = (Button) dialog.findViewById(android.R.id.button1);
            btn1.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));

            Button btn2 = (Button) dialog.findViewById(android.R.id.button2);
            btn2.setTypeface(FontHelper.getIranSansTypeface(MainActivity.this));
0

In order to achieve this, you need two things:

  • To create the AlertDialog via alertbuilder. Example from the official Android Documentation (http://developer.android.com/guide/topics/ui/dialogs.html):

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(R.string.dialog_fire_missiles)
           .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // FIRE ZE MISSILES!
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Create the AlertDialog object and return it
    return builder.create();
    
    • Set the Typeface of the TextView in your Alert Dialog as follows:

      AlertDialog dialog = builder.create();
      TextView textView = (TextView)  dialog.findViewById(android.R.id.message);
      Typeface face=Typeface.createFromAsset(getAssets(),"fonts/coolFont"); 
      textView.setTypeface(face); 
      dialog.show();
      
DimitrisCBR
  • 2,483
  • 1
  • 17
  • 19
  • I want to mention that you cannot get the values of the buttons and textviews before showing them creation of the dialog is not enough to get the values tested on API23 – MeqDotNet Feb 20 '17 at 17:48
  • Thanks for @MeqDotNet for this important point. If you try to find the message TextView before showing the AlertDialog, you get a NullPointerException these days. Call .show() first, then find it. – martianw Feb 02 '23 at 10:17
0
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/yourFONT"); 
textView.setTypeface(face);
Divyang Panchal
  • 1,889
  • 1
  • 19
  • 27