Richard
Richard

Reputation: 14635

Get resource id of attribute of view

Im creating a custom listview implementation. This is an instance of my view declaried in xml:

<CustomListView android:id="@+id/list" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:listSelector="@drawable/listview_background">
</CustomListView>

Is it possible to get the int value of the ’android:listSelector’ resource or any other attribute from my constructor?

public CustomListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.listSelectorId = ???
}

I have tried with this, but it does not return the correct resource id:

public CustomListView(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.selectableItemBackground});
    this.selectorResourceId = a.getResourceId(0, 0);
}

Upvotes: 2

Views: 2569

Answers (1)

Richard
Richard

Reputation: 14635

attr.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "listSelector", -1) did the trick :)

Upvotes: 3

Related Questions