pindol
pindol

Reputation: 2110

Remove all items inside 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

Upvotes: 78

Views: 66008

Answers (2)

MKJParekh
MKJParekh

Reputation: 34301

Why you wrote linearLayout.getParent()?

You should call this directly on LinearLayout:

linearLayout.removeAllViews();

Upvotes: 195

Manju
Manju

Reputation: 742

Hi Please try this code its working for me

public class ShowText extends Activity {
    /** Called when the activity is first created. */
    LinearLayout linearLayout;
    TextView textView,textView1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        textView=new TextView(this);
        textView1=new TextView(this);
        textView.setText("First TextView");
        textView1.setText("First TextView");

        linearLayout=(LinearLayout) findViewById(R.id.mn);
        linearLayout.addView(textView);
        linearLayout.addView(textView1);
        linearLayout.removeAllViews();

    }
}

Upvotes: 4

Related Questions