Typeface custom font doesn't work

575 views Asked by At

Typeface always shows default font.

My fonts are stored in the assets/fonts. And I have tried to use other fonts, and to re-encode fonts. Also PixlUI library didn't solve the problem.

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf");
    button.setTypeface(typeface);
}

activity_main.xml

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="220dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="doSomething"
    android:text="TEXT" />
3

There are 3 answers

5
Bradley Wilson On

If you haven't already tried using a font previously. I would suggest deleting your assets folder, creating a new assets folder inside your res folder and then move it back to where it was previously. Sometimes Android Studio doesn't accept the assets folder that gets built.

as shown here Runtime Exception: Font not found for every font i've tried - Android

also, I would reccomend creating a class that extends Button so that it'll automatically set your font for that widget if you assign the right XML to the button.

An example would be:

public class CustomFontButton extends Button {
    AttributeSet attr;
    public CustomFontButton(Context context) {
        super(context);
        setCustomFont(context, attr);
    }

    public CustomFontButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public CustomFontButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        String customFont = null;
        TypedArray a = null;
        if (attrs != null) {
            a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontButton);
            customFont = a.getString(R.styleable.CustomFontButton_customFont);
        }
        if (customFont == null) customFont = "fonts/OldEnglishFive.ttf";
        setCustomFont(ctx, customFont);
        if (a != null) {
            a.recycle();
        }
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e("textView", "Could not get typeface", e);
            return false;
        }
        setTypeface(tf);
        return true;
    }
}

and then in your xml you would only need to change

<yourpackagename.CustomFontButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="220dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:onClick="doSomething"
    android:text="TEXT" />

create an attrs.xml inside your values folder.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomFontButton">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources >

This would save you having to do setTypeFace everytime you would like a custom font on a button (or most other widgets, the same logic can be applied)

0
SachinSarawgi On

Instead of directly setting Typeface using setTypeface method, try below code snippet:

protected void onCreate(Bundle savedInstanceState) {
    ... //do whatever you want

    Button button = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf");

    button.setText(changeFontStyle("your text", typeface));
}

// custom function for changing the font style of text
public  Spannable changeFontStyle(String finalString, Typeface typeface){

    spannable = new SpannableString(finalString);
    spannable.setSpan(new CustomTypefaceSpan("", typeface), 0, finalString.length(), 0);

    return spannable;
}

Le me know if face any problem.

0
BlazeDev On

A good option would be to double check your folder, font and font directory name spellings. A good idea would be to show any errors that would've been printed and post them for us to understand the problem a little better. If you could also post the font name and double check if they are the same, and make sure that your assets are in the right location hope I helped.