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
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
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:
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.
textview1,textview2 and textView3than you should just use setTypface(yourfont) on them. Hope it helps.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.
<item name="android:textViewStyle">@style/MyTextView</item> ** A 'type' attribute is required for <item>** What I have to do now?type="string"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
You can extend the TextView as suggested here and set your typeface on that then use it in whole app.