Does making parent clickable make all child element clickable as well?

sandalone picture sandalone · Nov 15, 2011 · Viewed 14.8k times · Source

There is a LinearLayout with a lot of child elements. When a user touches any of those child elements, the same method will be invoked. In order not to implement the same onClickListener for each element, I implemented the onClickListener for the parent LinearLayout ONLY.

Now, when I click anywhere within the parent layout's borders, the desired method is being invoked just as I have implemented the listener for all child elements.

Q: Can I rely that anytime I implement onClickListener for the parent, all of its child elements will react to the click event?

Q: What would happen if any child element has its own onClickListener? Would there be a collision or clicking on that element would fire its own click event only?

Answer

Janusz picture Janusz · Nov 15, 2011

You answered your first question with your second question. A clickEvent will be delivered to the lowest child element in the layout hierarchy. If this element does not have an onClick behaviour it will pass the event up to its parent until the event gets handled.

Therefore you can treat the LinearLayout as one single block for your onClick behaviour. If you create another clickable element inside the layout be sure to make it big enough to reduce the chance of the user missing the correct item.