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

Community
  • 1
  • 1
Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78
  • 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. – suku Mar 07 '16 at 06:40
  • wouldn't you just use https://github.com/chrisjenx/Calligraphy – Fattie Apr 13 '17 at 00:29

5 Answers5

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.

Community
  • 1
  • 1
Vic Vuci
  • 6,993
  • 6
  • 55
  • 90
  • 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 – Andrea Mario Lufino Sep 05 '12 at 13:09
  • That solution doesn't seem to work anymore on Android 5.0 Any other option? – Azzedine Hassaini Dec 05 '14 at 13:40
  • @AndreaMarioLufino Setting this typeface in your adapter to all the textViews you got would work. Let's say you have `textview1,textview2` and `textView3`than you should just use setTypface(yourfont) on them. Hope it helps. – Recomer Jun 20 '16 at 08:20
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.

Community
  • 1
  • 1
Jelle
  • 919
  • 9
  • 16
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

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
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...

Aditya Nikhade
  • 1,373
  • 10
  • 15
0

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

Community
  • 1
  • 1
Amir
  • 203
  • 1
  • 8