6

I am trying to change the font of android.support.v7.app.AlertDialogtitle text.

METHOD 1 :

   TextView title = (TextView) dialog.findViewById(android.R.id.title); //returns null

METHOD 2 :

   final int titleId = context.getResources().getIdentifier("alertTitle", "id", "android");
   TextView title = (TextView) dialog.findViewById(titleId); //Also returns null.

Is there any other way to get the title TextView?

Please note I do not want to use a custom layout.

Thanks.

quad
  • 872
  • 3
  • 17
  • 37

4 Answers4

8

Use this one

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

Without any custom title :)

7

I got it to work using this solution :

    final AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);  

    Typeface tf = //get the typeface.
    CustomTFSpan tfSpan = new CustomTFSpan(tf);
    SpannableString spannableString = new SpannableString(title);
    spannableString.setSpan(tfSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    alertBuilder.setTitle(spannableString);

    AlertDialog dialog = alertBuilder.create();
    dialog.show();

CustomTFSpan

public class CustomTFSpan extends TypefaceSpan {

  private Typeface typeface;

  public CustomTFSpan(Typeface typeface) {
    super("");
    this.typeface = typeface;
  }

  @Override
  public void updateDrawState(TextPaint ds) {
    applyTypeFace(ds, typeface);
  }

  @Override
  public void updateMeasureState(TextPaint paint) {
    applyTypeFace(paint, typeface);
  }

  private static void applyTypeFace(Paint paint, Typeface tf) {
    paint.setTypeface(tf);
  }
}
Community
  • 1
  • 1
quad
  • 872
  • 3
  • 17
  • 37
1

Your question has already answer here : Change Title Font Of Alert Dialog Box Android

You can simply use a textview and set it as custom title like this : builder.setCustomTitle(tv2);

User
  • 31,811
  • 40
  • 131
  • 232
Sid
  • 13
  • 2
  • 12
  • I have seen his solution. It is similar to using custom layout. Is there a way to get the default title TextView? – quad Dec 01 '15 at 09:55
  • I don't think so there is any way to get the default one but what is the problem in this solution. You can set title from this and also you can change fonts. You can also try some third party dialogue library, may be this is the last option. – Sid Dec 01 '15 at 10:15
1

Create a simple TextView

TextView tv;

And replace

builder.setTitle("My Title");

with

builder.setCustomTitle(tv);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jayakrishnan Salim
  • 977
  • 1
  • 10
  • 24