Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Background Color of Clicked Child in Expandable ListView Android

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.

like image 896
Swayam Avatar asked Dec 27 '22 22:12

Swayam


2 Answers

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;
}
like image 186
Swayam Avatar answered Jan 18 '23 11:01

Swayam


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;
}
like image 44
Andro Selva Avatar answered Jan 18 '23 09:01

Andro Selva