I have on LinearLayout (I'll call it the parent) with inside an other LinearLayout (the child)
When I listen to a click on the child layout, I'd like to make the clickable animation on the parent and not only on the child.
How can I make that?
Thank you
One way to do it is to add a OnClickListener to the parent layout and then add the attribute android:duplicateParentState="true" to the inner layout. That way, when you press/click on the inner layout, the parent layout will also receive the event and process any animations and if it was being actioned directly.
Related
I have a child linearlayout created programmatically and added on a parent linearlayour programmatically.
parentlayout.addView(childlayout)
But, the I want to add again the child layout to another linearlayout, it returns an error, is it possible? What is the best way to do that?
Create another instance of the same class, and set the same properties. I mean Inflate your View again if needed. Because A single View cannot be a child of two ViewGroup parents.
Each view can have only one parent possible.
So you can not add same child to another linear layout.
What you can do is make another instance, apply same properties which you given to previous child. and then add this instance to the parent view.
In Android:
Given a View from an onTouchEvent, how can I remove the View and its parent?
I have a LinearLayout in which I have a View. Clicking on the View creates a sub-layout inside of the LinearLayout. That sub-layout needs its own onTouchListeners so I cannot attach an onTouchListener to the parent LinearLayout because that will always block onTouchListeners to my sub-layout.
Therefore, I need a means to remove the parent layout given its child layout. I can already remove child layouts from their parents.
If you know how to remove child views from a parent, stick with what you know ;)
The idea is to elevate to a position in the view tree where you can remove your parent view as a child. How do you do that? Easy, get the grandparent view of your view and remove your parent view from the grandparent.
((ViewGroup)view.getParent().getParent()).removeView((ViewGroup)view.getParent());
I have a ViewGroup that can have many LinearLayouts. And each LinearLayout can have many nested child views. What I want is that, if any view(even deep nested) inside a LinearLayout gets focus, it should call a custom method of its main parent LinearLayout.
The last thing I want to do is, set OnFocusChangeListener on every single deep nested child views of LinearLayout and that listener will call its parent's custom method. But that is really bad way to go for.
Is there any method I can override of parent LinearLayout that gets called every time its any nested child view's focus changes ?
You could use contentView.getViewTreeObserver().addOnGlobalFocusChangeListener() or override ViewGroup.requestChildFocus() of the root ViewGroup (requestChildFocus() is passed along the view parent chain).
I have a LinearLayout that acts like a container for views.
I'm inflating a different layout programatically and adding an OnClickListener and adding it in my LinearLayout.
What I want to achieve is to have the option to disable those OnClickListener on those views that I've inflated and added to my LinearLayout.
Can I just disable the OnClickListener on the parent LinearLayout to disable as well all the other child's OnClickListener?
Setting the parent LinearLayout click listener to null doesn't work though, any ideas?
THanks.
You can put the onClickListener as null to the views for which you don't want to add the listener instead of setting the listener as null to the parent layout.
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