How to avoid duplicate value for resource 'attr/*' in CustomView

1.2k views Asked by At

If I import:

CustomViewA (imported from Maven)

<declare-styleable name="CustomViewA">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>

CustomViewB (imported from Maven)

<declare-styleable name="CustomViewB">
        <attr name="min" format="float"/>
        <attr name="max" format="float"/>
</declare-styleable>

This will fail saying that min and max are duplicated. I thought Android would distinguish by the declare-styleable name, but guess not. Saying that, what's the best way to name a custom View attr to avoid any possible duplicate value clash in the future?

The only solution I got so far is:

<attr name="minForMyCustomViewHopingNoOneUsesThisName" format="float"/>

Which sucks.

2

There are 2 answers

0
Roman On

To make it unique is the ultimate answer. I prepare to use

<attr name="custom_view_a_min" format="float"/>
<attr name="custom_view_b_min" format="float"/>
6
Priyanka On

you can do this

<attr name="min" format="float" />
<attr name="max" format="float" />

<declare-styleable name="CustomViewA">
    <attr name="min" />
    <attr name="max" />
</declare-styleable>

<declare-styleable name="CustomViewB">
   <attr name="min" />
   <attr name="max" />
</declare-styleable>