[Solved] Multiple fonts to a single custom TextView


This is how I achieved:

  1. Created a custom TextView

    public class CaptainTextView extends TextView {
    private HashMap<String, Typeface> mTypefaces;
    
    public CaptainTextView(Context context) {
        super(context);
    }
    
    public CaptainTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    super(context, attrs, defStyleAttr);
    if (mTypefaces == null) {
        mTypefaces = new HashMap<>();
    }
    
    if (this.isInEditMode()) {
        return;
    }
    
    final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
    if (array != null) {
        final String typefaceAssetPath = array.getString(
            R.styleable.CaptainTextView_customTypeface);
    
        if (typefaceAssetPath != null) {
            Typeface typeface;
    
            if (mTypefaces.containsKey(typefaceAssetPath)) {
                typeface = mTypefaces.get(typefaceAssetPath);
            } else {
                AssetManager assets = context.getAssets();
                typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                mTypefaces.put(typefaceAssetPath, typeface);
            }
    
            setTypeface(typeface);
        }
        array.recycle();
    }
    }
    
    public CaptainTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (mTypefaces == null) {
            mTypefaces = new HashMap<>();
        }
    
        if (this.isInEditMode()) {
            return;
        }
    
        final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CaptainTextView);
        if (array != null) {
            final String typefaceAssetPath = array.getString(
                R.styleable.CaptainTextView_customTypeface);
    
            if (typefaceAssetPath != null) {
                Typeface typeface;
    
                if (mTypefaces.containsKey(typefaceAssetPath)) {
                    typeface = mTypefaces.get(typefaceAssetPath);
                } else {
                    AssetManager assets = context.getAssets();
                    typeface = Typeface.createFromAsset(assets, typefaceAssetPath);
                    mTypefaces.put(typefaceAssetPath, typeface);
                }
    
                setTypeface(typeface);
            }
            array.recycle();
        }
    }
    }
    
  2. Declared a custom attribute

    <resources>
    <declare-styleable name="CaptainTextView">
        <attr name="customTypeface" format="string" />
    </declare-styleable>
    </resources>
    
  3. Used in XML

    <com.project.captain.customviews.CaptainTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        android:textSize="16sp"
        android:textStyle="bold"
        app:customTypeface="fonts/Roboto-Thin.ttf" />
    

Voila!

solved Multiple fonts to a single custom TextView