Android route / bubble events because a control is consuming the onClick event - android

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?

View.onClick() event does not bubble. Two possible solutions:
Register OnCLickListener on you child views and then pass on the event by calling performClick() on parent.
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.

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?

Related

Android - Why setClicable(true) sets item non-clicable and vice versa?

According to developers' site View.setClicable(true) should
make the view clickable, false otherwise
, so why is it reversed in real? Only setClicable(false) makes my View clicable. Have I found a mistake in API description?
EVIDENCE:
gridview.getChildAt(position).setClickable(true);
Feel free to try it at home. After this the child in gridview won't be clicable.
I assume the clicking you're referring to is what you can listen to with an OnItemClickListener. This is handled entirely by the GridView (the GridView is also what draws the selector on the view), not the child view. You can control which items the GridView should consider as clickable with isEnabled in your adapter.
When you make the child view clickable it will handle all touch events over it. Because of this the GridView will not be notified of touch events on that view, and as such can't handle the click event.
So your view is perfectly clickable, you'd just have to use an OnClickListener to get notified of the clicks.
I think you have misunderstood what is is saying. It says exactly what you said. You pass true to make the view clickable or false otherwise (i.e to make it not clickable).

Issue with GestureListener

I am having a gesture listener attached to a view and I have onSingleTap Event handled.
It handles it properly if I tap anywhere in the view. But say if the View is having any subview and if I am tapping the subview the event does not get triggered.
Is there a way to pass this touch from child to parent? And also the children contains BUTTONS. so if the press is on a child button it should not pass the touch to parent. Otherwise it must pass it to parent. Anyway to achieve this?
You can override the Activity's dispatchTouchEvent(MotionEvent) in order to handle the dispatching yourself.
A good handling would be to first check if it triggers the gesture in your view and if not just call super.dispatchTouchEvent(MotionEvent);
The one thing you have to care about while doing this is to keep the gestures coherent with the rest of the platform.

ontouchlistener's range

I've got two views (one derived from SurfaceView another from ImageView) each encapsulated in a frame layout, and each view implements an ontouchlistener, which is set as the ontouchlistener of the view itself. These two views(layouts) have no intersection on the screen.
However, I cannot get these two listeners work together.
For example, if there is an ACTION_DOWN on view A (listener A), then listener B will not react (not events dispatched to B even the following motion is on view B), only listener A reacts until there is no touch events.
That is to say, even listener A is only set as the ontouchlistener for view A, it'll handle every event on the entire screen,and other listeners are inhibited.
That's strange to me, any explanations?
Can two ontouchlisteners exist together in on screen?
Of course they can, like two buttons with their own listeners. Try to set listeners to your views:
frameLayout.setOnClickListener();
imageView.setOnClickListener();
This way they should only react on events coming to their own listeners. Hope this helps.
android works as described here

The order of click event handled by parent view and child view

I have been working on android development for a while, but I am still confused about this question..
From android documentation said, the event will start from root view, and dispatched to the child view.
Which means viewgroup should get touch event before view(which is its child), but from what I have debugged, this is not the case...
I create a RadioGroup, then add three radiobutton into it, every time when I click radiobutton, its click event is triggered. Radiogroup's click event is not even fired...
How can we explain this?
Any idea? Thanks.
When a child handles the onClick, the parent does not receive the event. If you want to see the parent get the event, don't set an onClickListener on the child.
onTouch events are handled a bit differently.

Android Compound Control internal click events

I'm writing a custom compound control that extends RelativeLayout. Inside this component, there is an ImageView. I add a OnClickListener to this ImageView to animate it when the user clicks on it. But when I am in an activity using this control and I add a OnClickListener on the control, this listener is never called. It only works when I remove the other listener I have on the ImageView. Does anybody knows how to "propagate" the event to the other listeners when I catch it inside the control?
Thanks!
PS: I would also like to know if there is an existing control that looks like the icons on the Android desktop. Like an icon with text underneath.
Try setting one of these to true on your component:
android:clickable
android:focusable
android:focusableInTouchMode
android:longClickable
android:descendantFocusability
I had the same problem when I putted a button inside a ListView, I had to change one of the parameters to make the row be clickable, not only the button.

Categories

Resources