30

I'm trying to create a custom attribute called Tag for all editable elements. I added the following to attrs.xml

<declare-styleable name="Spinner">
    <attr name="tag" format="string" />
</declare-styleable>

<declare-styleable name="EditText">
    <attr name="tag" format="string" />
</declare-styleable>

I get an error saying "Attribute tag has already been defined" for the EditText. Is it not possible to create a custom attribute of the same name on different elements?

Arun
  • 3,036
  • 3
  • 35
  • 57

2 Answers2

70

If you are going to use an attr in more than one place then put it in the root element inside the <resources> element like the following :

<resources>
    
    <attr name="tag" format="string" />

    <declare-styleable name="Spinner">
        <attr name="tag" />
    </declare-styleable>

    <declare-styleable name="EditText">
        <attr name="tag" />
    </declare-styleable>

</resources>

Now you can use the tag attribute in anywhere you want inside this xml file.

starball
  • 20,030
  • 7
  • 43
  • 238
ninjasense
  • 13,756
  • 19
  • 75
  • 92
5

See if my detailed answer about custom attributes helps: Defining custom attrs

Community
  • 1
  • 1
Rich Schuler
  • 41,814
  • 6
  • 72
  • 59