26

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

Can anybody tell how can I do it?

3
  • stackoverflow.com/questions/16534888/… Commented Jun 4, 2013 at 9:16
  • Isn't there any simple way instead of creating custom dialog box ? Commented Jun 4, 2013 at 9:51
  • No. I Want To Change The Typeface Of The Title.. Commented Jun 4, 2013 at 10:04

14 Answers 14

31

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

Sign up to request clarification or add additional context in comments.

3 Comments

Acording to the doc... this also changes the dialog icon... so consider it beforce doing it...
so, how about buttons?how can i change the color or button of them?
@Eagle_Eye any Idea how to do this in bottom sheet dialog?
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(); 

4 Comments

Perfect Solution. It is working just fine. Thanks for the code Jared.!!!:)
It is perfect solution Jared, but doesn't work for Positive/Negative Button labels. Do have any similar solution for them?
@par4301 in DialogFragment#onStart() you can get a reference to the button and set the typeface. Example: ((AlertDialog getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setTypeface(tf)
@ParissaKalaee it works for buttons as well
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);

3 Comments

Yeah. Just want to add... you might do this: (TextView)findViewById(dialogTitle), as this alertTitle extends from TextView...
This worked great for getting the title of an EmailPreference dialog
Great! Also works with Unity Android Plugin Library's Native Java Code.
3

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

2 Comments

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"));
@Gaurav Arora any Idea how to change font in Bottom Sheet Dialog?
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.

3 Comments

But Will It Change The Font Of Title ?
in the TextView you can add the Font using the TypeFace class
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.
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" /> 

Comments

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

3 Comments

It's Confusing me.. A simple question.. Can i set Textview in place of TITLE ?
Which part is confusing? and is there any problem for using this.
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..
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) 

Comments

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.

1 Comment

Does not answer OP's question.
0

You can add this

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

after the line

alertDialog.show(); 

1 Comment

Invalid in 2020.
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.

Comments

0

Use this method from support library 26.

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

enter image description here

Read For more: Source

Comments

-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

4 Comments

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 ?
@DhavalAdesara surely.. instead of serif you can mention you custom font
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..?
@Pavan I don't think you are correct, according to CommonsWare, custom fonts can only be specified programmatically stackoverflow.com/questions/2562408/…
-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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.