attributes for multiple resources in attrs.xml

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

attributes for multiple resources in attrs.xml

Postby Vlemmix » Fri Apr 17, 2009 3:44 pm

Hello all,

I'm introducing multiple custom widgets for my application, extending View. These widgets (LineGraph and DotMatrix) include various attributes that can be set via the layout-xml. How can I have multiple widgets using attributes with the same name. For instance, 2 attributes both having color as an attribute that can be set via the layout XML?

main.xml snippet:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. xmlns:app="http://schemas.android.com/apk/res/net.vlemmix.android.widgetdemos"
  2. <net.vlemmix.android.widget.DotMatrix
  3.                 android:id="@+id/dotmatrix1"
  4.                 android:layout_width="wrap_content"
  5.                 android:layout_height="wrap_content"
  6.                 app:color="0xFFF" />
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


attrs.xml snippet:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <declare-styleable
  4.                 name="DotMatrix">
  5.                 <attr
  6.                         name="color"
  7.                         format="integer" />
  8.         </declare-styleable>
  9. </resources>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


When I want to add the info for my LineGraph in attrs.xml, Eclipse starts to whine: "ERROR Attribute "color" has already been defined"
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <declare-styleable
  4.                 name="DotMatrix">
  5.                 <attr
  6.                         name="color"
  7.                         format="integer" />
  8.         </declare-styleable>
  9. <declare-styleable
  10.                 name="LineGraph">
  11.                 <attr
  12.                         name="color"
  13.                         format="integer" />
  14.         </declare-styleable>
  15. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Is it not possible to have a DotMatix resource with a color attribute, and a LineGraph resource with a color attribute?
Vlemmix
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 17, 2009 3:40 pm

Top

Postby Vlemmix » Mon Apr 20, 2009 12:51 pm

Got some help from Dianne Hackborn:
> Just don't re-declare the format in the second one. Also, if this is a
> color, don't you want the format to be "color"? As it is now, when you
> write a color of the form "0xfff" you will not get the right integer value
> for an aarrggbb color.
>
> Oh and I am almost positive the framework already has a color attribute
> defines, so you could just use that.

So, once declared for a resource, you can declare the attribute names to resources, but do not include the format. So, once you declare "color" to be an int, "color" will be an int for all your resources (custom widgets). This forces that an attribute-name always refers to the same type, even between different resources.

And indeed, try to use the pre-defined attributes names/types when possible, no need to define my own color attribute...
Vlemmix
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 17, 2009 3:40 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Google Feedfetcher and 7 guests