Android - ExpandableListView change background for child items

10,579

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">
    <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

Share:
10,579
Bostone
Author by

Bostone

Most of my past experience is in Enterprise Java and related technologies. I started working with Java in 1996, prior to that I did about 5 years of C/C++ My last 8 years are all contract jobs for large clients, all loosely related to Spring framework and other various Open Source frameworks. Since 2008 I've been closely involved with Android development, authoring and writing Jobrio (a.k.a. Hire-A-Droid) and Droidin. I'm also collaborating with good folks of Flud reader to deliver Android version of their popular app I also (occasionally) maintain my dev blog

Updated on June 04, 2022

Comments

  • Bostone
    Bostone almost 2 years

    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 the 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 is 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?