Search code examples
androidxmlenumsdeclare-styleable

how to make enum values with | in them


I declared an android custom view that has an enum in it

   <attr name="ff_type" format="enum">
            <enum name="small" value="1" />
            <enum name="big" value="32" />
            <enum name="medium" value="8288" />

        </attr>

how to allow in my xml of the custom view to do app:ff_type="small|medium" ?


Solution

  • Use flag rather than enum:

    <attr name="ff_type" format="flag">
        <flag name="small" value="1" />
        <flag name="big" value="32" />
        <flag name="medium" value="8288" />
    </attr>
    

    Inclusion of format="flag" is optional.

    8288 is an odd choice, you're better sticking to powers of 2. As it stands 8288 = 32 * 259. Therefore you can't select medium without implying big.

    <attr name="ff_type">
        <flag name="small" value="1" />
        <flag name="medium" value="2" />
        <flag name="big" value="4" />
    </attr>
    

    Then you can optionally add additional values as shortcuts:

    <attr name="ff_type">
        <flag name="small" value="1" />
        <flag name="medium" value="2" />
        <flag name="big" value="4" />
        <flag name="smallerThanBig" value="3" />
    </attr>
    

    So here smallerThanBig is the same as small|medium (but you can use both).