Proper way to set the style of a custom typeface in Android

374 views Asked by At

I downloaded a font to include on my Android project.

Now, do I need to also include the "bold" and "italic" versions of the font to my assets or do I need only the regular one? And if I do so, do I need to manually setTypeface(getBoldFont(), Typeface.BOLD) for bold (and for italic) or does Android automatically get it done for me?

1

There are 1 answers

0
Mubashar Javed On

Download the following 4 types of the font: normal, bold, italic, bold_italic. Then create a CustomFont class as follows:

public class CustomFont {
    Context context;
    public CustomFont(Context context) {
        this.context = context;
    }

    public Typeface setNormalFont() {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(),"fonts/normal.ttf");
        return  typeface;
    }

    public Typeface setBoldFont () {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/bold.ttf");
        return typeface;
    }

    public Typeface setItalick() {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/italic.ttf");
        return typeface;
    }

    public Typeface setItalickBold() {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/italic_bold.ttf");
        return typeface;
    }
}

Then when you want to set font on any view, you can use the following code to use the desired font:

CustomFont customFont = new CustomFont(this);
textView.setTypeFace(customFont.setNormalFont());