Search code examples
androidbackgroundexpandablelistviewpressed

Change background of Expandable List child view when an element in child layout is clicked


I need to change the background of the child view in Expandable list view when child is clicked.

Child row layout is something like:

<RelativeLayout>     //Selector is placed for this layout
    ....
   <RelativeLayout>
      .... 
       <RelativeLayout>
         ....
         <TextView>
        ....
    ....

selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true"
        android:drawable= "@drawable/greyish"/>
</selector>

Now, when i clicked on TextView i want to change the background of whole row ( top most Relative layout comprise on every view with in child layout). How can i trigger the selector of top level relative layout.

Also, when OnChildClick() receives callback then row's background changes( because of selector placed in top level layout).

My attempt is:

In TextView's onlclick method:

   ((RelativeLayout)(nameView.getParent().getParent().getParent())).setPressed(true);

but this is not resulting in row layout to change background color.


Solution

  • Issue:

    ultimately you want to trigger the selector of parentView from its childView.

    Solution

    In your parent layout,add this line:

    android:addStatesFromChildren="true"
    

    and if you have the reference of parentView in your java code then you can achieve the task by using:

    parentView.setAddStatesFromChildren(true);
    

    where,parentView is your parent layout i.e.:RelativeLayout

    Note:

    make sure that your childview is not duplicating parent's state. i.e.: android:duplicateParentState or childView.setDuplicateParentState(true)

    I hope it will be helpful !