Hi,

Recently I was trying to configure the icons of an ExpandableListView.
As many people would do, I was trying to replace the default icons for
the minimized and maximized states. As it appears, doing this basic
thing it's not that straightforward as one would expect/guess. There
are not many examples in the forums either (at least I couldn't find
one). I would like to share a code excerpt which shows the steps for
replacing the default icons in an ExpandableListView. Please beware
that the code excerpt of this discussion has been pruned a lot, to
show only the steps relevant for replacing the default icons. Hope
that this example will be useful for people who are new in Android and
who wish to work with ExpandableListViews.

Here is the explanation:

Step-1: You need two icons under <YourCodeBase>\res\drawable. Let's
say that these icons are my_icon_min.png and my_icon_max.png.

Step-2: The way to override the default icons of an ExpandableListView
lies in creating a Drawable object which has states (minimized and
maximized, or expanded and collapsed), and set this object as the
group indicator of your ExpandableListView. We need a way for creating
the needed Drawable object with states. One decent way of doing this
is through an XML file. Android SDK provides an example XML file for
this purpose. Under Windows for Android-1.5 the location is android-
sdk-windows\platforms\android-1.5\data\res\drawable
\expander_group.xml. Since you're interested in replacing the default
icons, probably you'll need to copy this file under <YourCodeBase>\res
\drawable, edit it and put in the names of your icons (my_icon_min and
my_icon_max). See below:

expander_group.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android";>
    <item
        android:state_expanded="true"
        android:drawable="@drawable/my_icon_max" />
    <item
        android:drawable="@drawable/my_icon_min" />
</selector>

Step-3: Next comes the coding part. Below is a super simple example:

public class MyExpandableList extends ExpandableListActivity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
        .
        .
        .
    // Create a Drawable object with states
    Drawable icon =
this.getResources().getDrawable(R.drawable.expander_group);

    // Set the newly created Drawable object as group indicator.
    // Now you should be seeing your icons as group indicators.
    getExpandableListView().setGroupIndicator(icon);
        .
        .
        .
  }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to