views:

865

answers:

2

I'm using ExpandableListView in my app and one of the complaints I get from the users is that when the list item is expanded it's hard to visually distinguish where the child item ends and next group item begins. So I would like to change background of the child list item to the different shade. Brutal attempts that I've made so far were based on directly changing background color and text of the elements inside the child view item but that leads to loss of hovers and highlights. So my question is - what is a good strategy to achieve the above? I tried styles and selectors but what really bums me - if I change background for child item then I need to add selectors for all combinations of focus/enabled etc. when all I'm trying to do it to overwrite a single thing.

Is there a way to inherit parent style and set background only for non- focused, enabled child item with other styles retained?

A: 

Well. Here's what worked for me:

  1. Create list_background.xml in your project's res/drawable directory
  2. Set background value of the top level layout of your child item to the drawable above. If you are scratching your head at this point - I'm referring to the xml layout file that get's used when you create child view of your expandable list in ExpandableListAdapter#getChildView

Here's complete drawable file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <item android:state_pressed="true" android:state_enabled="true"
        android:drawable="@drawable/list_highlight_active" />
    <item android:state_enabled="true" android:state_selected="true" 
        android:state_window_focused="true"
    android:drawable="@drawable/list_highlight_inactive" />
    <item android:state_enabled="true" android:state_window_focused="true" 
        android:drawable="@color/item_body" />
    <item android:drawable="@color/item_body" />
</selector>

I had to copy list_highlight_active.xml and list_highlight_inactive.xml from /android-sdk-windows-1.6_r1/platforms/android-1.5/data/res/drawable to the drawable directory of my project. @color/item_body is just a shade of gray

DroidIn.net
A: 

I just discovered a way to "somewhat" set the background color without having to jump through the selector hoops.

As you've noted, setting the background to a solid color wipes out the selector highlights because the new background color obscures it. Droidin has what is the normal solution: provide your own background selector with the exact colors you want. It's a pain to say the least.

But if all you want is a little color differentiation then there is a simpler way: alpha blend. Set your background color with an alpha value. For example, set background to "#BBFFFFFF". The first two digits indicate the alpha level. It will all blend so the background won't be a pure white and the selection highlight won't be the normal bright orange but the children will be a different color and the highlight still works. Win-win.

Walt Armour