I read in another answer that in android you can declare attributes for your custom view that have multiple formats like this:
<attr name="textColor" format="reference|color"/>
how can I access these attributes in my class? Should I just assume it to be a reference, use getResources().getColorStateList()
and then assume it to be a raw RGB/ARGB color if Resources.getColorStateList()
throws Resources.NotFoundException
or is there a better way of differentiating between the formats/types?
In your /res/attrs.xml:
In your custom view constructor, try something like that (I didn't run it):
It should be something like this:
Variant 1
Of course getMultiColourAttr() method could be not static and not protected, this is up to the project.
The idea is to get some resourceId for this specific custom attribute, and use it only if the resource is not color but TypedValue.TYPE_REFERENCE, which should means that there is Drawable to be obtained. Once you get some Drawable should be easy to use it like background for example:
mView.setBackground(drawable);
Variant 2
Looking Variant 1 you can use the same resId but just pass it to the View method setBackgroundResource(resId) and the method will just display whatever stays behind this resource - could be drawable or color.
I hope it will help. Thanks