Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypedArray .getColor() always returning -1 in custom view

I've created a custom view which subclasses TextView called TextViewEx. This view simply adds more flexibility over the compound Drawables available to TextView via the xml. Part of the functionality I'd like to as is the ability to tint the compound drawables, but for whatever reason the color returned is always -1. Here's the code:

attrs.xml:

<resources>
    <declare-styleable name="TextViewEx">
        <attr name="drawableLeftWidth" format="dimension" />
        <attr name="drawableLeftHeight" format="dimension" />
        <attr name="drawableLeftTint" format="color" />

        <attr name="drawableTopWidth" format="dimension" />
        <attr name="drawableTopHeight" format="dimension" />
        <attr name="drawableTopTint" format="color" />

        <attr name="drawableRightWidth" format="dimension" />
        <attr name="drawableRightHeight" format="dimension" />
        <attr name="drawableRightTint" format="color" />

        <attr name="drawableBottomWidth" format="dimension" />
        <attr name="drawableBottomHeight" format="dimension" />
        <attr name="drawableBottomTint" format="color" />
    </declare-styleable>
</resources>

TextViewEx.java:

public class TextViewEx
        extends TextView {

    public TextViewEx(Context context) {
        super(context);
        init(null, 0);
    }

    public TextViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public TextViewEx(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs, defStyle);
    }

    private void init(AttributeSet attrs, int defStyle) {

        if (attrs==null) {
            return;
        }

        TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.TextViewEx, defStyle, 0);

        int lTint = -1, lWidth = -1, lHeight = -1;
        int tTint = -1, tWidth = -1, tHeight = -1;
        int rTint = -1, rWidth = -1, rHeight = -1;
        int bTint = -1, bWidth = -1, bHeight = -1;

        try {
            lTint = a.getColor(R.styleable.TextViewEx_drawableLeftTint, getCurrentTextColor());
            lWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableLeftWidth, 0);
            lHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableLeftHeight, 0);

            tTint = a.getColor(R.styleable.TextViewEx_drawableTopTint, getCurrentTextColor());
            tWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableTopWidth, 0);
            tHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableTopHeight, 0);

            rTint = a.getColor(R.styleable.TextViewEx_drawableRightTint, getCurrentTextColor());
            rWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableRightWidth, 0);
            rHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableRightHeight, 0);

            bTint = a.getColor(R.styleable.TextViewEx_drawableBottomTint, getCurrentTextColor());
            bWidth = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableBottomWidth, 0);
            bHeight = a.getDimensionPixelSize(R.styleable.TextViewEx_drawableBottomHeight, 0);
        } finally {
            a.recycle();
        }

        Drawable[] drawables = getCompoundDrawables();
        if (drawables[0]!=null && lWidth!=0 && lHeight!=0) {
            drawables[0].setBounds(0, 0, lWidth, lHeight);
        }
        if (drawables[1]!=null && tWidth!=0 && tHeight!=0) {
            drawables[1].setBounds(0, 0, tWidth, tHeight);
        }
        if (drawables[2]!=null && rWidth!=0 && rHeight!=0) {
            drawables[2].setBounds(0, 0, rWidth, rHeight);
        }
        if (drawables[3]!=null && bWidth!=0 && bHeight!=0) {
            drawables[3].setBounds(0, 0, bWidth, bHeight);
        }

        if (drawables[0]!=null && lTint!=-1) {
            Graphics.tint(drawables[0], lTint);
        }
        if (drawables[1]!=null && tTint!=-1) {
            Graphics.tint(drawables[1], tTint);
        }
        if (drawables[2]!=null && rTint!=-1) {
            Graphics.tint(drawables[2], rTint);
        }
        if (drawables[3]!=null && bTint!=-1) {
            Graphics.tint(drawables[3], bTint);
        }

        setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);

    }
}

and in use:

    <com.cheerfulinc.flipagram.view.TextViewEx
        android:id="@+id/likesCount"
        android:gravity="center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="30dp"
        android:layout_marginBottom="30dp"
        android:background="@drawable/red_pill_text"
        android:textColor="@android:color/white"
        android:text="22,4456"
        android:drawableRight="@drawable/fg_icon_heart"
        android:drawablePadding="6dp"
        app:drawableRightWidth="12dp"
        app:drawableRightHeight="12dp"
        app:drawableRightTint="#FFFFFFFF" />

I've also tried doing: app:drawableRightTint="@android:color/white" but no mater what, the color value returned is always -1

like image 888
Brian Dilley Avatar asked May 25 '15 01:05

Brian Dilley


2 Answers

I was using white, white is -1 :) The code above works just fine. I switched -1 for Integer.MAX_VALUE and all is good.

like image 63
Brian Dilley Avatar answered Oct 20 '22 01:10

Brian Dilley


Usw getColorStateList instead of getColor

like image 32
Muhannad Fakhouri Avatar answered Oct 20 '22 00:10

Muhannad Fakhouri