I am using the template layout which I import into different LinearLayouts. The template has a button in it.
When a user presses the button, I need to know in which LinearLayout the click event has occurred. Is this possible?
I am getting problem firing parent's event as when you press the child element, the parent's event will not fire at all.
you can get parent of any view using method:
http://developer.android.com/reference/android/view/View.html#getParent%28%29
Related
I just experimented with setting an OnClickListener to a parent View and an OnClickListener for every children. Without the children listener the parent listener would fire at any point in the layout but when I added children listeners only the listener associated with the child at that position fired. Is this the expected behavior? Where is this documented?
In an hierarchy of views starting with a parent view, a click event is "absorbed" by the 1st visible and enabled view (at the lower level) positioned at the physical area of the click event that has a registered listener.
Of course if needed, the invoked listener of that view may (or may not) trigger the click event of other views in the hierarchy but this is not the usual practice. For documentation, you may read this:
https://developer.android.com/guide/topics/ui/ui-events
especially the section "Event handlers", where there are mentioned ways for changing the above described default way of handling input events.
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.
I have a RelativeLayout that contains a few items: An ImageView and a few small TextView's.
Functionally I want to have the same on click event fire when anything in the RelativeLayout is clicked.
Visually I want to see the background of the RelativeLayout change so that it shows the entire layout (or "item") is being clicked.
My problem is that every time I click on the TextView's the on click doesn't propagate back to the parent view and so the background color doesn't change. How can I do this?
Ensuring you got no OnClickListener assigned to any of the childs of your RelativeLayout shall usually suffice for them to not receive clicks. Also check if you got no android:clickable="true" set by any chance for it. Then once you assing OnClickListener to your RelativeLayout it should get all the clicks.
for some items that has internal OnClickListener and you cannot easily remove their implementation of OnClickListener like SwitchComat, you can set
android:descendantFocusability="blocksDescendants"
on your parent layout. by adding this attribute to the parent view, non of the children will receive click events regardless of having onClickListener or not.
more on descendantFocusability
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?
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.
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.