How to remove sublayout from layout in Android?

I am new to Android. Let someone give some ideas for my problem.

/* Parent Linear Layout */ final LinearLayout par_layout=new LinearLayout(this); par_layout.setOrientation(LinearLayout.VERTICAL); /* Child Linear Layout */ final LinearLayout chl_layout=new LinearLayout(this); chl_layout.setOrientation(LinearLayout.VERTICAL); TextView tv_name=new TextView(this); tv_name.setText("Name "); TextView tv_item=new TextView(this); tv_item.setText("Items "); Button btn_submit=new Button(this); btn_submit.setText("Submit"); btn_submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub par_layout.removeAllViewsInLayout(); } }); chl_layout.addView(tv_name); chl_layout.addView(tv_item); chl_layout.addView(btn_submit); par_layout.addView(chl_layout); setContentView(par_layout); 

In the above code, when I click the button, I want to clear chl_layout from par_layout. But I can not. Can someone give some ideas?

Note: The following code also does not work

  par_layout.removeView(chl_layout); 
+4
source share
4 answers

Use the code below to remove the child view from the parent view.

  par_layout.removeView(chl_layout); 
+2
source

to make the whole layout empty there is a void function ...

LinearLayout li = new LinearLayout (this);

li.removeAllViews ();

+2
source

Try using this:

 fatherLayout.removeViewInLayout(childLayout); 
0
source

I cannot remove the child view (TableRow) from my parent (TableLayout) using removeView (View view). But addView works. Strange ...

-1
source

Source: https://habr.com/ru/post/913539/


All Articles