Reputation: 287
i'm try apply new font in my applcation android and can call from style.xml
eg. my font is in assets/thaoma.ttf
style.xml
<style name="text_black_color_bold" >
<item name="android:textColor">#3E3E3E</item>
<item name="android:textStyle">bold</item>
<item name="android:typeface"></item> /* call new font here */
</style>
Upvotes: 0
Views: 407
Reputation: 658
If you want to set this custom font to the entire app, and I think that's your case, create an assets directory and put your customefont.ttf file inside that directory.
After that, you need this lib compile it in your grade file
complie'me.anwarshahriar:calligrapher:1.0'
and use it in the onCreate method in the main activity
Calligrapher calligrapher = new Calligrapher(this);
calligrapher.setFont(this, "yourCustomFontHere.ttf", true);
This is the most elegant super fast way to do that.
Upvotes: 0
Reputation: 36045
You can't include it from xml. You have to do it through code like so:
final TextView myTextView = getTextView();
final AssetManager assets = ctx.getAssets();
final Typeface font = Typeface.createFromAsset(assets, "thamoa.ttf");
setTypeface(font);
One nifty trick is to extend TextView and auto-apply the font at runtime.
public class CustomTextView extends TextView {
public CustomTextView(Context ctx) {
super(ctx);
setupText(ctx);
}
public CustomTextView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
setupText(ctx);
}
public CustomTextView(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
setupText(ctx);
}
private void setupText(Context ctx) {
// check if in edit mode and return. Fonts can't be applied when viewing from editor
if(isInEditMode()) {
return;
}
final AssetManager assets = ctx.getAssets();
final TypeFace font = Typeface.createFromAsset(assets, "thamoa.ttf");
setTypeface(font);
}
}
Then you can use it the same way, but refer to it like so in xml:
<package.containing.class.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- whatever attributes that would normally apply here -->
/>
Upvotes: 1