5

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.

GuilhE
  • 11,591
  • 16
  • 75
  • 116

2 Answers2

5

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>
Priyanka
  • 3,369
  • 1
  • 10
  • 33
  • Will this work if I'm the `CustomViewA` owner but not from `CustomViewB`? Assuming that the `CustomViewB` developer uses the `attr` outside of the `declare-styleable` and/or inside? – GuilhE Feb 21 '20 at 12:05
  • can't understand. what are you saying? – Priyanka Feb 21 '20 at 12:07
  • Let's say that in my custom lib I use your approach. Then I import another Lib that uses the same approach. Will it collide? – GuilhE Feb 21 '20 at 13:30
  • Sorry for the delay. No, it will not collide. It will be working fine if both libs have the same `attr elements` – Priyanka Feb 24 '20 at 04:10
  • yes, it will work fine with a different format and the same name in a different module – Priyanka Feb 24 '20 at 11:12
  • Unfortunately this will not work if CustomViewA (maven import) uses your approach but CustomViewB (maven import) does not. Just tested. – GuilhE Feb 26 '20 at 12:33
0

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"/>
Roman
  • 2,464
  • 2
  • 17
  • 21