Question

I am trying to declare properties for to different classes some of the properties have the same name but different types on different classes. Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="net.firouz.mastergardner.IntEditView">
        <attr name="caption" format="string" />
        <attr name="min_val" format="integer" />
        <attr name="max_val" format="integer" />
    </declare-styleable>
    <declare-styleable name="net.firouz.mastergardner.FloatEditView">
        <attr name="min_val" format="float" />
        <attr name="max_val" format="float" />
    </declare-styleable>    
</resources>

but eclipse complains that the attributes max_val and min_val have already been defined. How can I fix this.

Thank you Sam

Was it helpful?

Solution 3

I found a better answer with the help of this posting. Apparently attributes can have multiple formats which makes the following code valid and can work for me.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="min_val" format="float|integer|reference" />
    <attr name="max_val" format="float|integer|reference" />
    <declare-styleable name="IntEditView">
        <attr name="Caption" format="string|reference" />
        <attr name="min_val" />
        <attr name="max_val" />
    </declare-styleable>
    <declare-styleable name="FloatEditView">
        <attr name="min_val" />
        <attr name="max_val" />
    </declare-styleable>    
</resources>

Thank you everyone

OTHER TIPS

You can go with either of below 2 solution

<?xml version="1.0" encoding="utf-8"?>
<attr name="min_val" format="float" />
<attr name="max_val" format="float" />
<resources>
    <declare-styleable name="net.firouz.mastergardner.IntEditView">
        <attr name="caption" format="string" />
        <attr name="min_val" />
        <attr name="max_val" />
    </declare-styleable>
    <declare-styleable name="net.firouz.mastergardner.FloatEditView">
        <attr name="min_val" />
        <attr name="max_val" />
    </declare-styleable>    
</resources>

or

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="net.firouz.mastergardner.IntEditView">
        <attr name="caption" format="string" />
        <attr name="min_val_i" format="integer" />
        <attr name="max_val_i" format="integer" />
    </declare-styleable>
    <declare-styleable name="net.firouz.mastergardner.FloatEditView">
        <attr name="min_val_f" format="float" />
        <attr name="max_val_f" format="float" />
    </declare-styleable>    
</resources>

android generated only one class for all attributes "R.attr", and as min_val and max_val were already defined as integer eclipse complain next time it sees its declaration as float.

Add your attributes directly as children of the node:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="min_val" format="float" />
    <attr name="max_val" format="float" />

    <declare-styleable name="net.firouz.mastergardner.IntEditView">
       <attr name="caption" format="string" />
       <attr name="min_val" />
       <attr name="max_val"/>
   </declare-styleable>

   <declare-styleable name="net.firouz.mastergardner.FloatEditView">
      <attr name="min_val" />
      <attr name="max_val"/>
   </declare-styleable>
</resources>

Else you have to use different attribute names for your float and integer attribute

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top