-6

How can we use different styles of Roboto(or any other) font (Roboto-Regular, Roboto-Thin, Roboto-Bold etc) in a single custom TextView.

One of the methods to do this programmatically is described here. But how to achieve this if we want to change style using XML.

Can this be done using custom attributes like this?

<com.project.abc.customviews.XYZTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome"
        app:type="thin" />

I don't want to make different java files for each type. Another method is described here but this has issues regarding memory. Looking for a good solution.

iamgopal
  • 634
  • 5
  • 12

1 Answers1

-1

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!

iamgopal
  • 634
  • 5
  • 12