How to pass a view's onClick event to its parent on Android?

The name of the picture


How to pass a view's onClick event to its parent on Android?



I have a TextView in a layout whos background is a Selector. And the TextView's text is set to Spanned from HTML.
Then I set the TextView with the LinkMovementMethod.



Now when I tap on the TextView, the click event is not sent to its parent layout to trigger the selector.



How should this be solved?




6 Answers
6



I think you need to use one of those methods in order to be able to intercept the event before it gets sent to the appropriate components:



Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.


Activity.dispatchTouchEvent(MotionEvent)



ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.


ViewGroup.onInterceptTouchEvent(MotionEvent)



ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).


ViewParent.requestDisallowInterceptTouchEvent(boolean)



More information here.



Hope that helps.





Is there an easier way to pass the events to trigger the onClick and onLongClick in parent view, or I have to implement them myself?
– shiami
Dec 12 '10 at 13:59





Check this out: stackoverflow.com/questions/2136696/pass-event-to-parent-view Possibly you might want to try passing the same click listener to the other instances as suggested, even though I don't know if it will work, but might be worth a try.
– Luis Miguel Serrano
Dec 12 '10 at 16:58





Answer below is much cleaner
– JPM
Dec 23 '13 at 19:00



Declare your TextView not clickable / focusable by using android:clickable="false" and android:focusable="false" or v.setClickable(false) and v.setFocusable(false). The click events should be dispatched to the TextView's parent now.


TextView


android:clickable="false"


android:focusable="false"


v.setClickable(false)


v.setFocusable(false)


TextView



Note:



In order to achieve this, you have to add click to its direct parent. or set
android:clickable="false" and android:focusable="false" to its direct parent to pass listener to further parent.


parent


android:clickable="false"


android:focusable="false"





Oh man if I could give this +100 I would. Been trying to figure out how to make a click only work in a ViewPager and not in the adapters layout with TouchImageView implementing onTouch! This solution worked like a charm.
– JPM
Dec 23 '13 at 18:59





Works perfectly. Much better than the excepted answer.
– Rooster242
Jun 16 '14 at 19:04





The scenario is : I have a layout which contains edittext ,relative layout, spinner and list view. I want to fire touch event of parent layout whenever i touch on anywhere on the screenexcept the edittext and spinner
– SweetWisher ツ
Jan 28 '15 at 9:35





Something important to bear in mind is that if you use TextView#setOnClickListener() on the TextView, it becomes clickable, even if it's declared as android:clickable="false", and even if the the ClickListener is set to null (setOnclickListener(null))
– Christian García
May 8 '15 at 10:39




TextView#setOnClickListener()


TextView


android:clickable="false"


ClickListener


null


setOnclickListener(null)





For some reason this didn't work for me
– Kevin Parker
Oct 10 '16 at 18:52



Sometime only this helps:


View child = parent.findViewById(R.id.btnMoreText);
child.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
View parent = (View) v.getParent();
parent.performClick();
}
});



Another variant, works not always:


child.setOnClickListener(null);



Put


android:duplicateParentState="true"



in child then the views get its drawable state (focused, pressed, etc.) from its direct parent rather than from itself.
you can set onclick for parent and it call on child clicked



If your TextView create click issues, than remove android:inputType="" from your xml file.


TextView


android:inputType=""





I dont know why, but this did the trick for me. Thank you!
– Marko Jovanović
Feb 25 at 10:51





If you don't know why, I recommend you don't do it. Just saying...to each his own.
– dell116
2 days ago





If you want to both OnTouch and OnClick listener to parent and child view both, please use below trick:


OnTouch


OnClick



User ScrollView as a Parent view and inside that placed your child view inside Relative/LinearLayout.



Make Parent ScrollView android:fillViewport="true" so View not be scrolled.


android:fillViewport="true"



Then set OnTouch listener to parent and OnClick listener to Child views.
And enjoy both listener callbacks.


OnTouch


OnClick






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to scale/resize CVPixelBufferRef in objective C, iOS

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

SVG with two text elements. When one resizes due to textLength - how to resize the other one to the same character size