Custom Attribute Error - Android Studio 1.2
Asked Answered
C

2

8

In my Android project I have a couple of custom components that use custom attributes.

The attrs.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources >
    <declare-styleable name = "TextBox">
        <attr name = "font" format = "string"/>
    </declare-styleable>

    <declare-styleable name = "ButtonBox">
        <attr name = "font" format = "string"/>
    </declare-styleable>
</resources>

I am pulling in the attributes just fine in the custom component, but when I go to run the code I see the following error.

Error: Found item Attr/font more than one time
Error: Execution failed for task ':app:mergeDebugResources'.

It shouldn't make a difference that there are similar attribute names in two different declare-styleable resources correct?

If you have any help it would be greatly appreciated, thank you!

Colugo answered 4/5, 2015 at 20:4 Comment(1)
Similar to #4434827Earle
L
7

As you can see here, the attr itself can have multiple properties and can be defined only once and you can configure multiple details inside it. So you should give it different names or, since they have the same properties, use only one declare-styable for both.

Check out this link too, there's a good example.

Here is how it should be:

<?xml version="1.0" encoding="utf-8"?>
<resources >
    <declare-styleable name="Box">
        <attr name="font" format="string"/>
    </declare-styleable>
</resources>

You can use Box on text, button, etc.

Ludewig answered 4/5, 2015 at 20:15 Comment(1)
Oh yes I guess I could make them use the same one, I didn't think about having the declare-styleable be any name other than it's association. These links were very helpful, thank you.Colugo
P
1

Android — Custom Views: same Attribute on multiple Custom Views

You may have tried something like the code below in your attrs.xml:

'''

<resources>
  
  <declare-styleable name="MyComponent1">
    <attr name="myAttr" format="string" />
  </declare-styleable>
  
  <declare-styleable name="MyComponent2">
    <attr name="myAttr" format="string" />
  </declare-styleable>
  
</resources>

'''

But this does not work, and we get an Error: Found item Attr/myAttr more than one time.

How do native views handle that? Native Views have the same attribute names and don’t suffer from this problem. Let’s sneak into their code and see how these attributes are declared.

Looking into their code, we can see that they create the attr tag outside declare-styleable, and then, inside of it you just need to reference it only by its name, there is no need to declare format again.

Our code should be just like that:

  <resources>
  
  <attr name="myAttr" format="string" />
  
  <declare-styleable name="MyComponent1">
    <attr name="myAttr" />
  </declare-styleable>
  
  <declare-styleable name="MyComponent2">
    <attr name="myAttr" />
  </declare-styleable>
  
</resources>

can also read this fantastic medium paper : same Attribute on multiple Custom Views

Prevailing answered 17/8, 2022 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.