I’ve been using custom attributes for some custom layout components without problem. So far, I’ve only used simple attributes (string, int, etc). These are defined like so in values/attrs.xml:
<declare-styleable name="StaticListView">
<attr name="border_size" format="dimension" />
</declare-styleable>
and in my layout:
<de.example.androidapp.StaticListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
namespace:border_size="1px"
/>
and used like so:
int borderSize = (int) a.getDimension(R.styleable.StaticListView_border_size, 0);
Now, I’m trying to specify a layout as a custom attribute and am not able to use the R.styleable method used above.
Here’s how I’m defining the attribute:
<declare-styleable name="StaticListView">
<attr name="emptyLayout" format="reference" />
</declare-styleable>
and using it:
<de.example.androidapp.StaticListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
namespace:emptyLayout="@layout/empty"
/>
And this is how I would like to use it, but I always get the default value (-1):
int emptyLayoutInt = attrs.getAttributeResourceValue(R.styleable.StaticListView_emptyLayout, -1);
This, however, works:
int emptyLayoutInt = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/de.example.androidapp", "emptyLayout", -1);
I dislike having to hardcode the XML namespace. Using the R.styleable attribute avoids this in a nice way.
Am I doing something wrong or is this a bug/expected behavior?
Instead of using the line :
Use this –
The -1 is getting returned because the value is not available in the attr set.