8

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)?

Tomasz Madeyski
  • 10,742
  • 3
  • 50
  • 62

5 Answers5

9

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.

Community
  • 1
  • 1
Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
0

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>
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62
-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;
    }
}
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
  • Now that we have to do a check for bold (or italic) text styles, the code is going to be a little longer. Since we don't want to repeat this code throughout each Activity or Fragment that applies our custom font, I prefer to put the code in the Application class. – Ajith V M May 27 '15 at 06:32
  • Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP. – User3 Sep 23 '15 at 09:04
  • A code block alone does not provide a good answer. Please add explanations. – Louis Barranqueiro Dec 18 '15 at 09:23
-3
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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP. – User3 Sep 23 '15 at 09:04
-4
info1 = (TextView) findViewById(R.id.textView1);
Typeface font = Typeface.createFromAsset(this.getAssets(),
            "fonts/KeepCalm-Medium.ttf");
info1.setTypeface(font);
info1.setTextColor(Color.parseColor("#FFFFFF"));
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
  • You can use this method to Add Custom Font from assets. First we download any font for example KeepCalm-Medium.ttf then the font willl be save in assets folder u can easly solve the problem – Ajith V M May 27 '15 at 06:19
  • Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP. – User3 Sep 23 '15 at 09:03