5

I have a custom view with an ImageView and a TextView on it and implemented the onClickListener for my custom view. The problem is, that the ImageView is consuming the onClick-event (I just want the user be able to click on my control, no matter where). I could listen to the onClick of the Image/TextView too, but it seems dirty to me.

Is there a way to bubble / route Events in Android? Or possible another good solution?

animuson
  • 53,861
  • 28
  • 137
  • 147
ShadowMare
  • 2,087
  • 2
  • 25
  • 27

2 Answers2

12

View.onClick() event does not bubble. Two possible solutions:

  1. Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.

  2. Use OnTouchListener which bubbles up: just return false in child view's onTouch() method. This is more work as you have to account for touch-down & lift-up in order to emulate click.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • Thank you for your explanations. I'll try a combination of your and Pedros answer! – ShadowMare Jan 18 '11 at 17:42
  • If the parent is being covered by children then this is a usable solution. Simply setting "clickable" alone wont do for those situations, but will aid if parts of the parent are reachable. Here is what worked for me and requires the least code: http://stackoverflow.com/a/18959830/189764 – Atorian Apr 29 '14 at 23:36
4

Have you set the onClickListener in your custom view?

Set your custom view as clickable.

I don't recommend on setting any click listener in the child views.

Does it work now?

Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37