4

This is more "Why?" than "How?" question, since I know a simple workaround for the problem. But I'd like to know what logic is behind behavior I'm going to describe.

I've got a style declaraion and some ListViews. I want to change the way divider looks. I used following declaration:

<style name="Theme.MyApp.Dark" parent="@style/Theme.Sherlock">
    <item name="android:dividerHeight">4px</item>
    <item name="android:divider">#123456</item>
</style>

To my amusement, only the height of the divider was applied. I've tried specifying color with alpha value (#12345678) and gradient drawable, nothing works. ListViews are declared like this:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

So, as you can see there's nothing that could override style declaration. I can change the divider only when I specify it directly in ListView declaration:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"  
    android:divider="#123456"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

Using my newly-acquired skill of specifying colors depending on theme selected, I can work around this very easily - I will just have to declare android:divider with custom color attribute for every list.

But why I can't change android:divider in the style, just like the android:dividerHeight? Is there some reason for that? I couldn't find an explanation, bug report or something similar that would let me understand this behavior.

EDIT: The theme is applied globally, in the Manifest file:

<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="org.my.app" android:theme="@style/Theme.MyApp.Dark">
Community
  • 1
  • 1
user1234567
  • 1,832
  • 1
  • 18
  • 25

2 Answers2

5

It can be set from a theme but with a little trick:

<style name="MyTheme" parent="AppTheme">
        <item name="android:listViewStyle">@style/MyListViewStyle</item>
</style>

<style name="MyListViewStyle" parent="@android:style/Widget.ListView">
        <item name="android:divider">#F00</item>
        <item name="android:dividerHeight">1px</item>
</style>

Rgrds ;)

Mark
  • 5,466
  • 3
  • 23
  • 24
1

You have to mention the style in the Lisview like this

   <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list"  
        style = "@style/Theme.MyApp.Dark"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/> 

Otherwise this wont work..

droid kid
  • 7,569
  • 2
  • 32
  • 37
  • The style is globally specified in the Manifest file. If it wouldn't be applied at all, the dividerHeight wouldn't work too. – user1234567 Dec 26 '11 at 14:55
  • Also did u try referring the parent attribute in the style??? like parent="@style/Theme.Dark" – droid kid Dec 26 '11 at 15:02