tags:

views:

111

answers:

1

Can I use my own resource names in themes.xml? For example:

<item name="headerBackground">@drawable/header</item>

In this example, "headerBackground" is not a valid resource ("No resource found that matches the given name...", says Eclipse).

Where should I create/declare these resources? Is this a bad idea?

+2  A: 

Of course you can use your own names, otherwise you'd be stuck with the Android default drawables.. :)

You declare drawable names by placing a file with that name in your res/drawable directory.


Updated, actual answer:

You need to declare a styleable attribute, in res/values/attrs.xml (the actual filename doesn't matter, but this seems to be the convention):

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="MyCustomView">
    <attr name="headerBackground" format="reference" />
  </declare-styleable>
</resources>

Then you can assign it a value in your theme, for example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme" parent="android:Theme">
        <item name="headerBackground">@drawable/header</item>
    </style>
</resources>

Of course whatever View you're implementing to use this newly-defined attribute has to be able to load that Drawable and apply it. The Gallery example on the Android Developers' site is useful there.

Christopher
Oh. Maybe the question was not clear. The problem here is that "headerBackground" is not a valid resource type. My question is where do I declare my own resource types - the names of the resources, and not the resource values.
moraes