Android - How To Set A Custom Font For Whole App
Solution 1:
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
TextViewtext= (TextView) findViewById(R.id.custom_font);
Typefacefont= Typeface.createFromAsset(getAssets(), "yourfont.ttf");
text.setTypeface(font);
With your .ttf in the root of the "assets" folder.
Solution 2:
You could do it like this. Create a custom textview and use that one everywhere
publicclassMyTextViewextendsandroid.widget.TextView
{
publicMyTextView(Context context)
{
this(context, null);
}
publicMyTextView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
publicMyTextView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
if (this.isInEditMode()) return ;
finalTypedArraya= context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
finalStringcustomFont= 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-styleablename="SomeStyle"><attrname="font"format="string" /></declare-styleable>
Then in your theme, do this: This will make sure all the textviews will use the style MyTextView
<itemname="android:textViewStyle">@style/MyTextView</item>
And now we can define your custom font using your custom attribute in this style.
<stylename="MyTextView"parent="@android:style/Widget.TextView"><itemname="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.
Solution 3:
Cerate a TextView subclass and use it everywhere
publicclassRobotoTextViewextendsTextView {
publicRobotoTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
publicRobotoTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
publicRobotoTextView(Context context) {
super(context);
init();
}
privatevoidinit() {
if (!isInEditMode()) {
Typefacetf= 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
Solution 4:
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...
Solution 5:
You can extend the TextView as suggested here and set your typeface on that then use it in whole app.
Post a Comment for "Android - How To Set A Custom Font For Whole App"