44

I want to use custom components in my project and i want to add it to enum attributes like below , how can i do that ?

<com.abb.abbcustomcompanents.buttons.AbbButton
        android:id="@+id/abbBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        app:Type="How can i use enum here"
        />

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="abbButton">
        <attr name="Type" format="enum"/>
        <attr name="onAction" format="string"/>
    </declare-styleable>
</resources>

Thank you !

2
  • 1
    check this stackoverflow.com/a/15231645/1329126 Commented Apr 11, 2013 at 8:18
  • Thanks it is acceptable answer ! if you write it as an answer i can mark it as accepted . Commented Apr 11, 2013 at 8:53

2 Answers 2

74

Ex :

<attr name="myProperty" format="enum">
         <enum name="None" value="0"/>
         <enum name="One" value="1"/>
         <enum name="Two" value="2"/>
         <enum name="Three" value="3"/>
</attr>

Use like this:

<YourCustomView
    ...
    app:myProperty="One"/>

Reference

https://stackoverflow.com/a/15231645/1329126

Sign up to request clarification or add additional context in comments.

2 Comments

How do I get this on my custom view?
@NeonWarge A bit late I know but for future I've add a use case
4

Order inside the XML matters, at least to eclipse. Define your enum above (or inside) your declare-styleable... not below.

<attr name="quality">
    <enum name="Good" value="1" />
    <enum name="Better" value="2" />
    <enum name="Best" value="3" />
</attr>

<declare-styleable name="SquareView">
    <attr name="quality" />
</declare-styleable>

<declare-styleable name="CircleView">
    <attr name="quality" />
</declare-styleable>

I had a very long enum so I placed it at the end of my XML to improve readability. It would parse correctly but reject values in Design mode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.