Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 317
Glen Pierce Avatar asked Sep 28 '15 19:09

Glen Pierce


1 Answers

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());
like image 143
thomaspsk Avatar answered Oct 20 '22 12:10

thomaspsk