Android – Remove all items inside linearlayout

androidandroid-linearlayout

I create a linearlayout that refes to an xml item. Inside this linearlayout i put some textview dynamically, so without taking them from the xml. Now i need to remove these textviews from the linearlayout. I tried this:

if(((LinearLayout) linearLayout.getParent()).getChildCount() > 0)
    ((LinearLayout) linearLayout.getParent()).removeAllViews();

but it doesn't work.
How can i do?
Thanks, Mattia

Best Answer

Why you wrote linearLayout.getParent()?

You should call this directly on LinearLayout:

linearLayout.removeAllViews();