Android: Let Parent View Handle On Click - android

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

Related

OnLongClick() blocks the other clicks on the parent view

I have a strange behavior, I use a RecyclerView to display a list.
Each item of this list is composed of a LinearLayout containing 2 TextViews. (It's a classic configuration).
I have an OnClickListener on the LinearLayout and an OnLongClickListener on the second TextView.
The problem is that the OnLongClickListener prevents the "normal clicks" to reach the LinearLayout.
To summarize:
If I click on the first TextView, the Layout is clicked.
If I click on the second TextView, nothing happens.
You can't avoid this situation with your current xml file, you may need to make adjustment
Though if you want it with this design, you have to add OnClickListener to your second text that have OnLongClickListener and call inside it whatever you call on linearView.OnClickListener .
When you clicked on TextView just disable all click of linear layout because at a time one click listener will work.
linearlayout.setClickable(false);
linearlayout.setEnabled(false);

Does making parent clickable make all child element clickable as well?

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.

Cannot trigger onClickListener on a LinearLayout with linkfied TextViews

I'm developing a list view in which each cell is a LinearLayout with other views inside it. I have also set the onClickListener of the cells to take the user to another Activity.
The problem is that one of the views inside is a TextView in which I apply the Linkify function. When the TextView happens to have a link in its text, I cannot trigger the onClickListener anymore, unless I click on another view of the LinearLayout. This problem also applies to the highlighting feature.
Does anyone knows what may be happening?
Thanks!
If you are applying the Linkify function inside of your getView() Override, I would wonder if it's just automatically setting the "Clickable" type methods on the view being passed to it. Right after Linkify, you could try calling setClickable(false), setFocusable(false), setFocusableInTouchMode(false) all on the view that was Linkified.

Android, Layout clicklistener and subview click through problem

I have a RelativeLayout to which I add buttons and set their onCLickListener to the current Activity where I handle their clicks.
Under a particular circumstance I need to set the RelativeLayout onClickListener also, but then once finished with the required clicking on the layout, I need to allow clicking on the buttons again. (i.e. clicking throug hthe layout)
If I set the layout's click listener to null I can no longer click either the layout or the buttons that are it's child views.
What am I doing wrong?
EDIT: I seem to have fixed it by setting;
relativeLayout.setClickable(false);
Have you tried:
relativeLayout.setOnClickListener(null);
relativeLayout.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
?

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