Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add multiple fonts to font text view class?

I have a question regarding adding multiple custom fonts to textview. I have basically added the fonts in fonts folder and have created a java class for fonttextview based on the solutions i found online. However I see they have added only one font and I want to add multiple fonts like roboto-regular,roboto-bold,cabin-bold etc. Here's the code I have so far:

public class FontTextView extends TextView {


    public FontTextView(Context context) {
      super(context);
      Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
      this.setTypeface(face);

    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
  this.setTypeface(face); 
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
     Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf"); 
  this.setTypeface(face); 
    }

How do I go about it creating multiple fonts? Also, I tried the styleable etc, but it shows error as it doesnt support styleable class, can anyone add another font to this existing code and walk me through the retrieval process?

Thanks! Justin


1 Answers

Use the following code for different fonts set to the xml file.

public class CustomTextView extends TextView {
private static final String TAG = "CustomTextView";

public CustomTextView(Context context) {
    super(context);
}

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

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

private void setCustomFont(Context ctx, AttributeSet attrs) {
    TypedArray a = ctx.obtainStyledAttributes(attrs,R.styleable.CustomTextView);
    String customFont = a.getString(R.styleable.CustomTextView_customFont);
    setCustomFont(ctx, customFont);
    a.recycle();
}

public boolean setCustomFont(Context ctx, String asset) {
    Typeface tf = null;
    try {
    tf = Typeface.createFromAsset(ctx.getAssets(), "fonts/"+asset);  
    } catch (Exception e) {
        Log.e(TAG, "Could not get typeface: "+e.getMessage());
        return false;
    }

    setTypeface(tf);  
    return true;
}

}

And in the xml file you can use it as:

<com.package_name.CustomTextView
           your_name:customFont="arialbd.ttf" />

and int the main parent layout add

xmlns:your_name="http://schemas.android.com/apk/res/com.package_name"

and remember to add a attrs.xml in values folder, with following resource in it

<resources>
<declare-styleable name="CustomTextView">
    <attr name="customFont" format="string"/>
</declare-styleable>

Hope it helps

like image 71
AnujMathur_07 Avatar answered Jan 30 '26 20:01

AnujMathur_07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!