Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 495
pindol Avatar asked Mar 20 '12 10:03

pindol


People also ask

What is a linearlayout?

The LinearLayout is the most basic layout, and it arranges its elements sequentially, either horizontally or vertically. To arrange controls within a linear layout, the following attributes are used:

How to align the button controls to the center of linearlayout?

To align the Button controls Mango and Banana to the center and to the right of the LinearLayout container, add the following statements to the respective tags in the activity_linear_layout_app.xml layout file:

How do you arrange controls in linear layout?

The LinearLayout is the most basic layout, and it arranges its elements sequentially, either horizontally or vertically. To arrange controls within a linear layout, the following attributes are used: The orientation attribute is used to arrange its children either in horizontal or vertical order.

How to make the controls expand vertically in a linearlayout?

Because the three Button controls are arranged vertically in the layout (the orientation of the LinearLayout is set to vertical), the application of the weight attribute makes the controls expand vertically instead of horizontally as we saw earlier. To see the effect, let’s add the following statement to the tags of all three Button controls:


2 Answers

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();      } } 
like image 36
Manju Avatar answered Oct 08 '22 09:10

Manju


Why you wrote linearLayout.getParent()?

You should call this directly on LinearLayout:

linearLayout.removeAllViews(); 
like image 128
MKJParekh Avatar answered Oct 08 '22 07:10

MKJParekh