Glen Pierce
Glen Pierce

Reputation: 4841

How to remove a parent view - android

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.

Upvotes: 7

Views: 12816

Answers (1)

thomaspsk
thomaspsk

Reputation: 789

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());

Upvotes: 26

Related Questions