4

I want to change the background color of the child which is clicked in an ExpandableListView. That is, when any child is clicked, it's background color should get changed. I am trying to go it in this way but it selects some other row, not the one which has been clicked.

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    parent.getChildAt(childPosition).setBackgroundColor(Color.DKGRAY);

    return false;
}

Please tell me what I might be missing.

Swayam
  • 16,294
  • 14
  • 64
  • 102

3 Answers3

6

This is how I solved it.

View _lastColored;
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {
    _groupPosition = groupPosition;


    if(_lastColored != null)
    {
    _lastColored.setBackgroundColor(Color.TRANSPARENT);
    _lastColored.invalidate();
    }
    _lastColored = v;
    v.setBackgroundColor(Color.rgb(214, 214, 214));


    return false;
}
Swayam
  • 16,294
  • 14
  • 64
  • 102
  • GOOD PIECE OF CODE THANKS! – Nitesh Verma Aug 27 '14 at 04:59
  • @NiteshVerma : Thank you! Glad it could be of help. :) – Swayam Aug 27 '14 at 05:14
  • @Swayam : Nice code.But Is it possible to change the background color of a child view outside the onChildClick() ? – Parag Kadam Aug 16 '15 at 19:43
  • @ParagKadam : Can you please elaborate what you mean by "outside the onChildClick()"? I mean, you would need the reference of the view. As long as you have the reference of the view whose background you want to change, you can do it anywhere. – Swayam Aug 17 '15 at 04:38
  • I saved the view that I got inside the onChildClick() , and I tried to set background color to it but unfortunately nothing happens. – Parag Kadam Aug 17 '15 at 06:28
  • @Swayam : Here is the link http://stackoverflow.com/questions/32044616/change-background-color-of-clicked-child-in-expandable-listview-android-outside – Parag Kadam Aug 17 '15 at 07:14
  • @Swayam :It worked but the child selected at one group is also selected in other group.How to solve this issue? – Nischal Sep 23 '15 at 10:23
  • Shouldn't really happen. And if it does, add a check for groupPosition. – Swayam Sep 23 '15 at 14:08
3

How about lets say you try to directly refer your view and set the background like this,

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

         v.setBackgroundColor(Color.BLUE);

    return false;
}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 1
    Yep! This works, but the color persists even when another child is clicked. My aim here is to highlight which child has been selected, so when another is selected, others should get back to normal. – Swayam Jun 16 '12 at 12:41
  • 1
    Then you have to manually find out which child is clicked and set the color to it and remove the color from the previously selected child. You can either maintain the position of which child is being clicked or use some other better strategy to achieve this. – Andro Selva Jun 16 '12 at 12:45
1

I think you should use

public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {


    Object obj = parent.getTag();
      if(obj instanceof View){
             ((View) obj).findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.<your normal color>);
         }


    v.findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.DKGRAY);


 parent.setTag(v);



    return false;
}


parent.getChildAt(childPosition).findViewByID(<id of main ViewGroup of row >).setBackgroundColor(Color.DKGRAY);

or 


v.findViewByID(<id of main ViewGroup of row>).setBackgroundColor(Color.DKGRAY);

for second one http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html#onChildClick(android.widget.ExpandableListView, android.view.View, int, int, long)

Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • Can you please explain the findViewByID part ? – Swayam Jun 16 '12 at 12:26
  • I had problem with making item selceted this way is when I was using in simple list that if you are using convert view in getView then on scrlloing down it shows other view as selected....... – Dheeresh Singh Jun 16 '12 at 12:52
  • Thank you so very much for your help. I already used Andro's technique and worked it out. – Swayam Jun 16 '12 at 14:38