25

I want to change the font of the title of Alert Dialog box.

Can anybody tell how can I do it?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Eagle_Eye
  • 1,044
  • 1
  • 14
  • 26

14 Answers14

30

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..

Eagle_Eye
  • 1,044
  • 1
  • 14
  • 26
17

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();
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
9

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

IndRaj
  • 3
  • 3
Rez
  • 4,501
  • 1
  • 30
  • 27
2

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.

droidchef
  • 2,237
  • 1
  • 18
  • 34
  • But Will It Change The Font Of Title ? – Eagle_Eye Jun 04 '13 at 09:25
  • in the TextView you can add the Font using the TypeFace class – droidchef Jun 04 '13 at 13:37
  • The title is a separate view in the AlertDialog. This would only work if you didn't use the AlertDialog's title and instead made a title in your custom view. Is that what you were trying to say above? Might want to mention that. Otherwise, I don't see how this would work. – stuckj Nov 11 '15 at 22:03
2

Do like this :

Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));

Have your font.ttf file in the assets folder and use it like above

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • 1
    I think this is appropriate method, though i would like to suggest one correction : TextView title = (TextView) dialog.findViewById(android.R.id.title); title.setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf")); – Pavan Jun 04 '13 at 12:35
  • @Gaurav Arora any Idea how to change font in Bottom Sheet Dialog? – Raunak Pandey Jul 23 '20 at 17:32
2

Instead of setting the text of the alertdialog, you should set a custom view form your layouts. And before you do so, modify your view's font. try this example

TextView content = new TextView(this);
     content.setText("on another font");
     content.setTypeface(Typeface.SANS_SERIF);

//Use the first example, if your using a xml generated view

 AlertDialog.Builder myalert = new AlertDialog.Builder(this);
     myalert.setTitle("Your title");
     myalert.setView(content);
     myalert.setNeutralButton("Close dialog", null);
     myalert.setCancelable(true);
     myalert.show();

code for xml...replace your font in the xml

<TextView
    android:id="@+id/yourid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dp"
    android:text="your content" />
1

Include this layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView
   android:id="@+id/text"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Hello_World"
   android:textColorLink="#FF00FF"
  />

</LinearLayout>

Then use it inside Ur Activity

Dialog dialog = new Dialog(ActivityName.this);
dialog.setContentView(R.layout.dialog_name);
dialog.setCancelable(true);
dialog.findViewById(R.id.text).setTypeface(Typeface.createFromAsset(getAssets(), "font.ttf"));


Another Way and this

Community
  • 1
  • 1
Sam
  • 1,124
  • 1
  • 8
  • 12
  • It's Confusing me.. A simple question.. Can i set Textview in place of TITLE ? – Eagle_Eye Jun 04 '13 at 10:33
  • Which part is confusing? and is there any problem for using this. – Sam Jun 04 '13 at 10:57
  • Sam.. I Was Confused About That Can We Use TextView To Set The Title Of Alert Box Instead Of Using builder.setTitle("Title")..! But Now I Used setCustomTitle() Instead Of setTitle().. And passed object of textview as argument.. And Now I Am Able Change Typeface Of Title By Changing The Typeface Of Textview.. Thanx A Lot For Helping Fellow.. – Eagle_Eye Jun 04 '13 at 17:52
1

You can try Spannable as well.

            SpannableString s = new SpannableString("My App");
            s.setSpan(new TypefaceSpan(this, "font.otf"), 0, s.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            s.setSpan(new RelativeSizeSpan(1.3f), 0,s.length(), 0);
            builder.setTitle(s)
samirprogrammer
  • 438
  • 5
  • 18
0

You can change appearence of AlertDialog by following this block of code:

AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Message").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(10);//to change font size

//to change font family
Typeface face = Typeface.createFromAsset(getAssets(),"font/fontFileName.ttf");
textView.setTypeface(face);

Put font file in assets folder. In my case I created a subdirectory called font.

And you can also check for this Question which have an accepted answer.

Community
  • 1
  • 1
Sanjay Joshi
  • 2,036
  • 6
  • 33
  • 48
0

You can add this

TextView textViewt = (TextView) alertDialog.findViewById(android.R.id.title);
textViewt.setTypeface(your font typeface);

after the line

alertDialog.show();
user1438038
  • 5,821
  • 6
  • 60
  • 94
B.Habibzadeh
  • 490
  • 6
  • 10
0

There is an easy way to set a new custom view to dialog's title. We can define every custom view such as TextView and add it some custom properties and at last set it to dialog's title, such as below:

 AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);



        TextView title_of_dialog = new TextView(getApplicationContext());
        title_of_dialog.setHeight(50);
        title_of_dialog.setBackgroundColor(Color.RED);
        title_of_dialog.setText("Custom title");
        title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        title_of_dialog.setTextColor(Color.WHITE);
        title_of_dialog.setGravity(Gravity.CENTER);

        builder.setCustomTitle(title_of_dialog);
        builder.create().show();

Here, I define a dynamic TextView and set some properties to it. Finally, I set it to dialog's title using "setCustomTitle()" function.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
masood
  • 193
  • 1
  • 1
  • 8
0

Use this method from support library 26.

ResourcesCompat.getFont(context, R.font.<YOUR_FONT>);

enter image description here

Read For more: Source

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
-1

Try setting theme for the dialog. Change the typeface attribute like this :

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:typeface">serif</item>
</style>

Though this changes Fonts of all the textviews within the dialog. so make sure TextViews are set to proper typeface

Pavan
  • 711
  • 2
  • 6
  • 17
  • Thanx for help Pavan.. But I am not using system font.. I am using another custom font.. so can i do this with custom font also ? – Eagle_Eye Jun 04 '13 at 09:48
  • @DhavalAdesara surely.. instead of serif you can mention you custom font – Pavan Jun 04 '13 at 12:32
  • But It's Showing Error.. That.. error: Error: String types not allowed (at 'android:typeface' with value 'Myfont.ttf'). Can You Explain How To Set Custom Font In Style..? – Eagle_Eye Jun 04 '13 at 17:34
  • @Pavan I don't think you are correct, according to CommonsWare, custom fonts can only be specified programmatically http://stackoverflow.com/questions/2562408/set-specific-font-in-a-styles-xml – Daniel Wilson Sep 29 '14 at 13:46
-1
TextView tv_message = new TextView(this);

                Typeface typeface = Typeface.createFromAsset(
                        getAssets(),
                        "fonts/OpenSans-Semibold.ttf"
                );


                // Set the text view layout parameters
                tv_message.setLayoutParams(
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                );

                // Set message text color
                tv_message.setTextColor(Color.RED);

                // Set message gravity/text align
                tv_message.setGravity(Gravity.START);

                // Set message text size
                tv_message.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);

                // Set message custom font
                tv_message.setTypeface(typeface);

                // Set message background color
                tv_message.setBackgroundColor(Color.YELLOW);

                // Set message text padding
                tv_message.setPadding(15, 25, 15, 15);

                tv_message.setText("Are you sure?");
                tv_message.setTextColor(Color.BLACK);
Umair
  • 6,366
  • 15
  • 42
  • 50