user256239
user256239

Reputation: 18027

Pass event to parent view?

I have a layout which contains five TextView. When user clicks any of the five TextView, it will do the same thing: open another activity. So, the OnClickerListener behavior to the five TextView is the same. Is it possible that I only add OnClickListerner to one view, e.g. to layout, so I don't have to add OnClickListerner to each of the five TextView? Put it another way, if none of the children components receives CLICK event, will the CLICK event passed through to their parent, the layout?

Thanks.

Upvotes: 2

Views: 10013

Answers (3)

aclima
aclima

Reputation: 780

It may not be the case but I recently spent a couple of hours trying to make a TextView propagate its click events to its parent. I wasn't being successful because apparently if you set the MovementMethod tv.setMovementMethod(LinkMovementMethod.getInstance()); it will catch the "bubbling-up" and never reach its parent.

Upvotes: 1

Michael Stoner
Michael Stoner

Reputation: 1401

The above answer from Mayra is not correct. If the lowest level control (i.e. textview in your case) does not have an OnClickListener() associated with it, then the click event "bubble up" to the parent. It will continue this way until a view is found that implements an OnClickListener; otherwise the click is ignored (I assume). I have taken advantage of this behavior many times.

Also, refer to this post for more details: Stackoverflow Question 8135032

I know this is a very old post, but wanted to correct it for anyone else that stumbles upon this question.

Upvotes: 7

Cheryl Simon
Cheryl Simon

Reputation: 46844

I don't believe there is another place to automatically receive click inputs from all views in a layout.

You could extend TextView with your custom OnClick behavior.

Or, just pass the same OnClickListener instance to all of your text boxes.

Upvotes: 1

Related Questions