Accessing XML enum values from code

1.6k Views Asked by At

I've declared a styleable attribute with enum values, like so:

<declare-styleable name="TileLayout">
    <attr name="rotation" format="integer">
        <enum name="top" value="0"/>
        <enum name="left" value="1"/>
        <enum name="right" value="2"/>
        <enum name="bottom" value="3"/>
    </attr>
</declare-styleable>

Now I'd like to reference those in my code, preferably within a switch statement.

I cannot find any way of doing this other than just hard-coding the values I have in the enum into my code (defeating half the purpose of the enum in the first place).

Does anyone know how to do this?

EDIT

Per @CommonsWare's answer, tried this:

resources.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="rotation_top">0</integer>
</resources>

attrs.xml:

<resources>
    <declare-styleable name="TileLayout">
        <attr name="rotation" format="integer">
            <enum name="top" value="@integer/rotation_top"/>
            <enum name="left" value="1"/>
            <enum name="right" value="2"/>
            <enum name="bottom" value="3"/>
        </attr>
    </declare-styleable>
</resources>

This caused top to no longer be an accepted value for rotation.

0

There are 0 best solutions below