25

Possible Duplicate:
Is it possible to set font for entire Application?

I need to set a custom font (.ttf format) for whole app, how can i do it? From Manifest or XML would be the best option if it is possible

2
  • Just because it is marked duplicate, one can't provide a better answer: Use the calligraphy library to achieve this. It even changes the font of toasts. Commented Mar 7, 2016 at 6:40
  • wouldn't you just use github.com/chrisjenx/Calligraphy Commented Apr 13, 2017 at 0:29

5 Answers 5

22

EDIT Sept 2014:

For anyone still seeing this old terrible answer, the real, good answer is here:

https://stackoverflow.com/a/16883281/1154026


OLD:

Your best bet would be this:

Customize Android Fonts

So

 TextView text = (TextView) findViewById(R.id.custom_font); Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf"); text.setTypeface(font); 

With your .ttf in the root of the "assets" folder.

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

3 Comments

This works great, but if I have a ListView how can i change the font of the row? I suppose is possible to do through XML
That solution doesn't seem to work anymore on Android 5.0 Any other option?
@AndreaMarioLufino Setting this typeface in your adapter to all the textViews you got would work. Let's say you have textview1,textview2 and textView3than you should just use setTypface(yourfont) on them. Hope it helps.
11

You could do it like this. Create a custom textview and use that one everywhere

public class MyTextView extends android.widget.TextView { public MyTextView(Context context) { this(context, null); } public MyTextView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs); if (this.isInEditMode()) return ; final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle); final String customFont = a.getString(R.styleable.SomeStyle_font); //Build a custom typeface-cache here! this.setTypeface( Typeface.createFromAsset(context.getAssets(), customFont) ); } } 

Add this to attrs.xml

<declare-styleable name="SomeStyle"> <attr name="font" format="string" /> </declare-styleable> 

Then in your theme, do this: This will make sure all the textviews will use the style MyTextView

<item name="android:textViewStyle">@style/MyTextView</item> 

And now we can define your custom font using your custom attribute in this style.

<style name="MyTextView" parent="@android:style/Widget.TextView"> <item name="font">MyPathToFontInAssets.ttf</item> </style> 

So whenever you use MyTextView in the project, it will have your custom font.

IMPORTANT: Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache so you can re-use the custom typeface for all textviews. This will significantly speed-up the application!

UPDATE: As Amir was saying, this is almost the same as Custom fonts and XML layouts (Android) but i also use android styling to automatically use it on all textviews in the app.

7 Comments

Hi! Its showing error in this line <item name="android:textViewStyle">@style/MyTextView</item> ** A 'type' attribute is required for <item>** What I have to do now?
I cleared errors by adding type="string"
Typefaces are not cached, so if you plan on using this code, you should also build a custom typeface-cache how to build a cache?
You can have a static singleton class somewhere with a hashmap that holds references to the loaded typefaces. Be sure to clear it when your app is closed!
<item name="font">MyPathToFontInAssets.ttf</item> how can i set the path here? any example? thanks~
|
10

Cerate a TextView subclass and use it everywhere

 public class RobotoTextView extends TextView { public RobotoTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } public RobotoTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public RobotoTextView(Context context) { super(context); init(); } private void init() { if (!isInEditMode()) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf"); setTypeface(tf); } } } 

usage

<com.test.RobotoTextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" /> 

in java code you can cast it to TextView

Comments

7
TextView tv=(TextView)findViewById(R.id.custom); Typeface face=Typeface.createFromAsset(getAssets(), "fonts/Verdana.ttf"); tv.setTypeface(face); 

put the font file in the fonts folder in /res/

Like my answer if it is helpful...

Comments

0

You can extend the TextView as suggested here and set your typeface on that then use it in whole app.

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.