Search code examples
androidandroid-studiocustom-componentattr

Custom Attribute Error - Android Studio 1.2


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!


Solution

  • 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.