Reputation: 10890
What I want to achieve is adding style
attribute to my axml
file with custom font loaded from Asset
.
I know I can load built-in fontface like this (style.xml
):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:typeface">monospace</item>
</style>
</resources>
And use it (Main.axml
):
<TextView
style="@style/CodeFont"
local:MvxBind="Text Hello" />
I know that I can also create MyCustomTextView
extending TextView
and setting font by SetTypeface
method, but I would like to use custom font in style
attribute.
So something like:
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:typeface">MyCustomFontLoadedFromAsset</item>
</style>
</resources>
Is it possible (and how to do it)?
Upvotes: 8
Views: 16631
Reputation: 5336
I know this is an old question, but nowadays you can do exactly that with the fontFamily
attribute (see the docs):
<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
<item name="android:fontFamily">@font/yourcustomfont</item>
</style>
Obviously you need to have previously defined that fontFamily
. To do so, please see the docs.
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="@font/yourcustomfont_regular" />
<font
app:fontStyle="italic"
app:fontWeight="400"
app:font="@font/yourcustomfont_italic" />
</font-family>
Upvotes: 0
Reputation: 1
public class MyApplication extends Application {
private Typeface normalFont;
private Typeface boldFont;
...
/**
* Fonts
*/
public void setTypeface(TextView textView) {
if(textView != null) {
if(textView.getTypeface() != null && textView.getTypeface().isBold()) {
textView.setTypeface(getBoldFont());
} else {
textView.setTypeface(getNormalFont());
}
}
}
private Typeface getNormalFont() {
if(normalFont == null) {
normalFont = Typeface.createFromAsset(getAssets(),"fonts/my_font.ttf");
}
return this.normalFont;
}
private Typeface getBoldFont() {
if(boldFont == null) {
boldFont = Typeface.createFromAsset(getAssets(),"fonts/my_font_bold.ttf");
}
return this.boldFont;
}
}
Upvotes: -1
Reputation: 1
info1 = (TextView) findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(this.getAssets(),
"fonts/KeepCalm-Medium.ttf");
info1.setTypeface(font);
info1.setTextColor(Color.parseColor("#FFFFFF"));
Upvotes: -4
Reputation: 1
String fontPath = "PT_Sans-Narrow-Web-Regular.ttf";
holder.desc = (TextView) row.findViewById(R.id.msgdesc);
// Loading Font Face
Typeface tf = Typeface.createFromAsset(
row.getContext().getAssets(), fontPath);
// Applying font
holder.desc.setTypeface(tf);
Upvotes: -3
Reputation: 7752
There isn't a way to do it straight out of the box. There's a library called Calligraphy that will let you do it. It will work on API 7+
I've also seen a trick using reflection to override serif/sans so that you can define those in your styles.xml
: Is it possible to set a custom font for entire of application?, though I would recommend the first approach.
Upvotes: 9