Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to set a custom font for whole app [duplicate]

Tags:

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

like image 551
Andrea Mario Lufino Avatar asked Sep 05 '12 12:09

Andrea Mario Lufino


People also ask

How do you change the font on all apps?

In the Action Launcher Settings menu, tap the “Appearance” option. Scroll down within the “Appearance” menu and then tap “Font.” Choose one of the custom Action Launcher fonts available within the “Font” menu. Tap on one of the options to confirm your choice and then select the back button to return to your app drawer.

What is Android App default font?

The fall and rise of Roboto, Android's default font.


2 Answers

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.

like image 180
VicVu Avatar answered Oct 24 '22 13:10

VicVu


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.

like image 21
Jelle Avatar answered Oct 24 '22 13:10

Jelle