Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create my own resource names?

Tags:

android

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?

like image 668
moraes Avatar asked Dec 20 '25 04:12

moraes


1 Answers

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.

like image 165
Christopher Orr Avatar answered Dec 22 '25 17:12

Christopher Orr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!