Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Make parent selector trigger when selecting child

Tags:

android

I have a LinearLayout with some other LinearLayouts inside. The children layouts have a background selector set. I need to change the color of the parent layout as well when a children is pressed. I have set a background selector as well for the parent, but it only triggers when clicking outside of any children.

How can I avoid this?

Thanks!

like image 934
markitusss Avatar asked May 15 '12 17:05

markitusss


1 Answers

Try adding this to the layout attributes for the parent LinearLayout:

android:addStatesFromChildren="true"

Alternatively, in code call:

parentLayout.setAddStatesFromChildren(true);

In either case, be sure that the child layouts do not have the android:duplicateParentState attribute set (and do not call setDuplicateParentState(true)), or you will get an exception at run time when the system detects the circular dependency.

like image 94
Ted Hopp Avatar answered Sep 28 '22 11:09

Ted Hopp