Somk
Somk

Reputation: 12047

Android Expandable ListView Icon (Group Indicator)

I have been able to successfully swap in my own image for the expandable listview arrows. I have two issues at the moment.

One is that the icons i substitue in with the code below are each strechted to the height of the row and too wide as well. The second is, that this code is only changing the initial state of the Group Indicator. How do I change it from one image to another when a particular row is open?

Drawable plus = (Drawable) getResources().getDrawable(R.drawable.plus);
getExpandableListView().setGroupIndicator(plus);

Upvotes: 1

Views: 3396

Answers (2)

thomas8wp
thomas8wp

Reputation: 2411

Android's default behavior is to stretch the groupIndicator icon. It's a ninepatch, so if you don't want it to stretch the image, you have to make the stretchable area of your ninepatch the transparent regions on both sides of the icon.

To make it change appropriately when the ExpandableListView opens, you have to use a StateListDrawable. Add the appropriate Drawables for the following states:

int[][] states = new int[][] {
        new int[] { android.R.attr.state_expanded },
        new int[] { },
};

You do that like so:

StateListDrawable sld = new StateListDrawable();
sld.addState(states[0], YOUR_EXPANDED_DRAWABLE);
sld.addState(states[1], YOUR_UNEXPANDED_DRAWABLE);

And set that to the groupIndicator.

Hope that helps.

Upvotes: 1

ahodder
ahodder

Reputation: 11439

What you could do, is onCollapse and onExpand change the group indicator drawable. Source

Upvotes: 0

Related Questions