Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding "items" in Android's style.

I have an Android library with themes, attributes, styles, colors and, of course, code.

For things like separator's height, colors of items etc.. i have the ability to override dimensions using dimensions.xml and color using colors.xml (leaving the layouts, theme and style alone).

However, if i have a ListView which uses Android's list_selector_background and i also have another activity which uses this selector for a press-able TextView, besides overriding it in code, I can't find a way to do it though of using the ?style:attrib syntax but i can't see how it helps

The simple and fastest solution i currently have is create a style:

<style name="ResultsListViewStyle" >
    <item name="android:listSelector">@android:drawable/list_selector_background</item>
</style>

and then copy paste the above piece of xml to my actual project style.xml and change the selector there. This is not so bad however i do need to do this for every item using this selector...

like image 701
codeScriber Avatar asked Jul 20 '11 14:07

codeScriber


2 Answers

Since no one picked the glove eventually I found the solution, indeed as an attribute.
The solution:

  1. In your attrs.xml create an entry for the value you need, for drawbale like selector for example use: <attr name="ListResultSelector" format="reference" />

  2. In your theme.xml define your theme, if that attribute is your only one and you want Android normal theme as your theme you can use:

    <style name="Theme" parent="@android:style/Theme.NoTitleBar">
      <item name="ListResultSelector">@android:drawable/list_selector_background</item>
    </style>
    
  3. If you want to override in a specific application you can simple inherit 'theme' (<style name="Theme.newTheme">) and change that specific property there.

  4. In your style instead of defining a drawable you can define: <....android:background="?attrib:ListResultSelector" ../>

  5. Apply the theme either to your application or to the relevant activity

In this way I don't need to override an entire style, only special properties like drawables, type faces etc... inside it. So if I ever need to change something in the original style and I want it to effect both the old and the new applications I don't need to do it twice.

like image 77
codeScriber Avatar answered Nov 13 '22 17:11

codeScriber


In correction to @codeScriber's answer:

Resources defined in themes can be referenced in layouts via ?attr/ListResultSelector or just ?ListResultSelector.

Also refer to the docs concerning this: http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes

like image 1
Brian Avatar answered Nov 13 '22 17:11

Brian