Custom Attributes for TextView Subclass not working

800 views Asked by At

I subclassed TextView to create my own TextView which uses custom fonts:

My custom textview class

`public class StyledTextView extends TextView {
int style;
static final int BOLD = 0;
static final int SEMIBOLD = 1;
static final int NORMAL = 2;
static final int LIGHT = 3;

    public StyledTextView(Context context) {
        super(context);
        style(context);
    }

    public StyledTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        style(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
        style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
        a.recycle();
    }

    public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        style(context);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
        style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
        a.recycle();
    }

    private void style(Context context) {
        Typeface tf;
        switch(style) {
            case BOLD:
                 tf = Typeface.createFromAsset(context.getAssets(),
                        "fonts/open-sans.bold.ttf");
                setTypeface(tf);
                break;
            case SEMIBOLD:
                tf = Typeface.createFromAsset(context.getAssets(),
                        "fonts/open-sans.semibold.ttf");
                setTypeface(tf);
                break;
            case NORMAL:
                tf = Typeface.createFromAsset(context.getAssets(),
                        "fonts/open-sans.regular.ttf");
                setTypeface(tf);
                break;
            case LIGHT:
                tf = Typeface.createFromAsset(context.getAssets(),
                        "fonts/open-sans.light.ttf");
                setTypeface(tf);
                break;
            default:
                tf = Typeface.createFromAsset(context.getAssets(),
                        "fonts/open-sans.regular.ttf");
                setTypeface(tf);
                break;
        }
    }
}'

My attrs file:

'<?xml version="1.0" encoding="utf-8"?>

<declare-styleable name="StyledTextView">
    <attr name="text_style" format="integer"/>
</declare-styleable>


<declare-styleable name="SlidingUpPanelLayout">
    <attr name="panelHeight" format="dimension" />
    <attr name="shadowHeight" format="dimension" />
    <attr name="paralaxOffset" format="dimension" />
    <attr name="fadeColor" format="color" />
    <attr name="flingVelocity" format="integer" />
    <attr name="dragView" format="reference" />
    <attr name="overlay" format="boolean"/>
    <attr name="anchorPoint" format="float" />
    <attr name="initialState" format="enum">
        <enum name="expanded" value="0" />
        <enum name="collapsed" value="1" />
        <enum name="anchored" value="2" />
        <enum name="hidden" value="3" />
    </attr>
</declare-styleable>


<declare-styleable name="ExtendedPagerSlidingTabStrip">

<attr name="indicatorColor" format="color"/>

<attr name="underlineColor" format="color"/>

<attr name="dividerColor" format="color"/>

<attr name="indicatorHeight" format="dimension"/>

<attr name="underlineHeight" format="dimension"/>

<attr name="mydividerPadding" format="dimension"/>

<attr name="tabPaddingLeftRight" format="dimension"/>

<attr name="scrollOffset" format="dimension"/>

<attr name="tabBackground" format="reference"/>

<attr name="shouldExpand" format="boolean"/>

<attr name="mytextAllCaps" format="boolean"/>

</declare-styleable>

'

Example Use of Styled TextView `

<UtilityClasses.StyledTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Suggested Channels"
    android:id="@+id/textView4"
    android:layout_alignParentTop="true"
    android:layout_marginTop="25dp"
    android:textSize="20sp"
    app:text_style="0"
    android:textColor="#787878"
    android:textAllCaps="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15dp" />

'

All StyledTextViews are using Normal font because of the fail case in

`style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);`

Which leads me to believe that the attributes are not retrieved properly in the xml, also I have other classes which have the same problem of custom attributes defined in xml having no effect
I've read through several online thread and it seems as tho including

xmlns:app="http://schemas.android.com/apk/res-auto" 

in the root view is the only thing necessary in the xml layout to use custom attributes If anyone can help it would be greatly appreciated!

1

There are 1 answers

0
user3711987 On

I figured out the answer!

I was getting the typed array and attributes after the style was already set

I changed:

public StyledTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    style(context);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
    style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
    a.recycle();
}

To this:

 public StyledTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
        try {
            style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
        } finally {
            a.recycle();
        }
        style(context);
    }