3

I'm trying to add a custom attribute (@drawable) to my custom View and I want to retrieve it as resourceId. Somehow my View cannot find it if I want to access it through code:

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TouchImageView, defStyle, 0);
int base = a.getResourceId(R.styleable.imageDrawable, R.drawable.kaart);

The error I got is: Cannot be resolved or is not a field

My attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TouchImageView">
        <attr name="imageDrawable" format="string"/>
  </declare-styleable>
</resources>

The xml in the layout would be:

<nl.raakict.android.util.TouchImageView
     android:id="@+id/plattegrond"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     imageDrawable="@drawable/kaart"/>
Bart Burg
  • 4,786
  • 7
  • 52
  • 87

1 Answers1

0

Ah it was pretty easy. Apparently the styleable had to be used as prefix to the attribute like so: TouchImageView_imageDrawable. So the new code would be:

int base = a.getResourceId(R.styleable.TouchImageView_imageDrawable, R.drawable.kaart);
Bart Burg
  • 4,786
  • 7
  • 52
  • 87