Passing style resource id from XML to class not working

Viewed 1944

I have a custom widget in a library project (Spinnerbutton) that I want to use in an application project. The custom widget contains a TextView and I want to pass a style to that TextView from my app project.

This is my attrs.xml (in the library project):

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

    <declare-styleable name="Spinnerbutton">
        <attr name="myTextAppearence" format="reference" />
    </declare-styleable>

</resources>

And the app's layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/lib/com.example.spinnerbuttonlib"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.spinnerbuttonlib.spinnerbutton.Spinnerbutton
        android:id="@+id/sbp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        custom:myTextAppearence="@style/SmallTextGray" />

</RelativeLayout>

Here's how I try to read my custom attribute in the Spinnerbutton class:

public Spinnerbutton(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;

    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.Spinnerbutton);

    int textStyleId = a.getResourceId(
            R.styleable.Spinnerbutton_myTextAppearence, -1);

    a.recycle();

}

textStyleId always return -1, so the value is not passed from my layout to the class.

What's wrong here?

5 Answers
Related