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
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:
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:
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.
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:
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(); } }
Why you wrote linearLayout.getParent()
?
You should call this directly on LinearLayout
:
linearLayout.removeAllViews();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With