Remove all items inside linearlayout

pindol picture pindol · Mar 20, 2012 · Viewed 58.2k times · Source

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

Answer

MKJParekh picture MKJParekh · Mar 20, 2012

Why you wrote linearLayout.getParent() you should do all this directly on LinearLayout

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