Assuming I have a custom attribute my_custom_enum for my views:
<attr name="my_custom_enum" format="enum">
<enum name="first_value" value="751"/>
<enum name="second_value" value="2222"/>
<enum name="third_value" value="1241"/>
<enum name="fourth_value" value="4"/>
</attr>
<declare-styleable name="CustomViewOne">
<attr name="my_custom_enum"/>
</declare-styleable>
<declare-styleable name="CustomViewTwo">
<attr name="my_custom_enum"/>
</declare-styleable>
is there a way to obtain all the possible values of this enum in code?
In other words:
I would like to have a way to obtain values 751, 2222, 1241 and 4.
names of those values would be nice too, but are not mandatory.
The solution I ended up with is the one suggested by pskink in the comment: parsing the
attrs.xmland extracting values myself.There are two reasons that made it perfectly reasonable to do it this way:
intsare used when usingAttributeSet.The code I ended up with is this:
This method returns a
Mapofname-valuepairs that represent an attribute that hasattrName.For the example I wrote in the question, you would use this method like this: