Change title font of Alert Dialog box

You have to inflate or customize and create a style and apply to AlertDialog

Heres how you inflate a layout and apply it to AlertDialog

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Formatted");
builder.setView(view);

define all the formatting and styles required in the layout you specified.

You can access specific textview defined in the layout using inflated View i.e.

LayoutInflater li = LayoutInflater.from(ctx);
View view = li.inflate(R.layout.formatted_dialog, null);
TextView label=(TextView)view.findViewById(R.id.i_am_from_formatted_layout_lable);

Sample layout saved as res/layout/link.xml:

In your onCreate() or where or whenever you want to call AlertDialog

LayoutInflater li = LayoutInflater.from(this);
View view = li.inflate(R.layout.link, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Formatted");
builder.setView(view).create().show();
TextView text=(TextView) findViewById(R.id.text);

replace this with context object if you are calling from some other method.


No answers provide a way to change the title typeface of an AlertDialog. Here is what I have done:

Create the below class:

public static class TypefaceSpan extends MetricAffectingSpan {

  private final Typeface typeface;

  public TypefaceSpan(Typeface typeface) {
    this.typeface = typeface;
  }

  @Override public void updateDrawState(TextPaint tp) {
    tp.setTypeface(typeface);
    tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
  }

  @Override public void updateMeasureState(TextPaint p) {
    p.setTypeface(typeface);
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
  }

}

Add the following utility method:

/**
 * <p>Return spannable string with applied typeface in certain style</p>
 *
 * http://stackoverflow.com/questions/8607707/how-to-set-a-custom-font-in-the-actionbar-title
 *
 * @param typeface
 *     The typeface to set to the {@link SpannableString}
 * @param string
 *     the string to place in the span
 * @return SpannableString that can be used in TextView.setText() method
 */
public static SpannableString typeface(Typeface typeface, CharSequence string) {
  SpannableString s = new SpannableString(string);
  s.setSpan(new TypefaceSpan(typeface), 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  return s;
}

Finally, set the typeface when creating your AlertDialog:

Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/your_font.ttf");
new AlertDialog.Builder(getActivity())
    .setTitle(FontUtils.typeface(typeface, "The title"))
    /* .. */
    .create();

I found a simple solution..

At first I was setting the title of my alert box by

builder.setTitle("My Title");

So I was not able to change the font of it..

Then what worked for me is..

I created a simple TextView :

TextView tv2;

And set all properties of TextView which I wanted...

And then I replaced my

builder.setTitle("My Title");

line with

builder.setCustomTitle(tv2);

and now I can change Title Color, Font Etc By Changing tv2's Properties..


Just use the following line to get an identifier to the dialog's title:

int dialogTitle = mCtnx.getResources().getIdentifier( "alertTitle", "id", "android" );

use this one for android.support.v7.app.AlertDialog

TextView title= (TextView)alertDialog.findViewById(R.id.alertTitle);