TypedArray getColor always returns defValue

Viewed 1545

In my XML element I try to pass through colors from the colors.xml resource file. the getColor function always returns the defValue, even though it should return the colors given in the colors.xml.

code

TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.accordion);
final int col_pressed = a.getColor(R.styleable.accordion_buttonColorPressed, Color.BLACK);
final int col_unpressed = a.getColor(R.styleable.accordion_buttonColorPressed, Color.YELLOW);
btn.setBackgroundColor(col_unpressed);

main XML

<mika.actual.AccordionWidget
    xmlns:accordion="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    accordion:text_color="@color/text_color"
    accordion:buttonColorPressed="@color/button_pressed"
    accordion:buttonColorUnpressed="@color/button_not_pressed"
    android:background="#FFFFFFFF"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"> ... </mika.actual.AccordionWidget>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <color name="text_color">#FFFFFF</color>
    <color name="button_pressed">#666666</color>
    <color name="button_not_pressed">#BBBBBB</color>
</resources>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="accordion">
        <attr name="text_color" format="color"/>
        <attr name="buttonColorPressed" format="color"/>
        <attr name="buttonColorUnpressed" format="color"/>
    </declare-styleable>
</resources>
1 Answers

It's an old thread but no one respond. I will, so viewers can have an answer I think you just have to change all your accordion: to app: like this

app:text_color="@color/text_color"
app:buttonColorPressed="@color/button_pressed"
app:buttonColorUnpressed="@color/button_not_pressed"
Related