Remove all items inside linearlayout

AndroidAndroid Linearlayout

Android Problem Overview


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

Android Solutions


Solution 1 - Android

Why you wrote linearLayout.getParent()?

You should call this directly on LinearLayout:

linearLayout.removeAllViews();

Solution 2 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpindolView Question on Stackoverflow
Solution 1 - AndroidMKJParekhView Answer on Stackoverflow
Solution 2 - AndroidManjuView Answer on Stackoverflow