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

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It supports API7+, Only the example is API16 :)
Thanks @Chris.Jenkins , I guess, as the author, you'd know! I've updated the answer.
@Ben Pearson How to Apply it for whole Application? Hop You will reply me fast!
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>

Comments

-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;
    }
}

3 Comments

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.
Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP.
A code block alone does not provide a good answer. Please add explanations.
-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);

1 Comment

Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP.
-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"));

2 Comments

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
Downvote, as this does not answer the question. This answer only repeats what has already been done by the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.