dynamically add remove control form linearlayout

android, android-linearlayout

Solution

Remove view from parent on Android:

View myView = findViewById(R.id.my_view);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);

Android remove all child views:

LinearLayout formLayout = (LinearLayout)findViewById(R.id.formLayout);
formLayout.removeAllViews();

Add view to parent on Android:

Button myButton = new Button(getApplicationContext());
myButton.setLayoutParameters(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

you can use:

LinearLayout.LayoutParams.FILL_PARENT

or

LinearLayout.LayoutParams.WRAP_CONTENT

Problem

I have 3 layouts, I need when click on button access certain layout and ad remove controls from and in it any idea how to achieve that , this is the code I use ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.20" android:orientation="horizontal" > <Button android:id="@+id/backbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back" /> <Button android:id="@+id/backbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="next" /> </LinearLayout> <!-- the two columns part --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.80" android:orientation="horizontal" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.80" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="First Name" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.20" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second Name" /> </LinearLayout> </LinearLayout> </LinearLayout> ```

Original source