Pankaj
Pankaj

Reputation: 8058

Android: How to change font family of AlertDialog message and buttons

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();

Upvotes: 3

Views: 10228

Answers (3)

Behnoud Sherafati
Behnoud Sherafati

Reputation: 719

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));

Upvotes: 2

Divyang Panchal
Divyang Panchal

Reputation: 1909

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);

Upvotes: 0

DimitrisCBR
DimitrisCBR

Reputation: 2553

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();
      

Upvotes: 0

Related Questions