6

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>
AMH
  • 6,363
  • 27
  • 84
  • 135

4 Answers4

21

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
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
K_Anas
  • 31,226
  • 9
  • 68
  • 81
  • why adding the button like that give error The constructor Button(new View.OnClickListener(){}) is undefined – AMH May 10 '12 at 06:47
  • in the android Api there isn't a constructor like you have done There is only these 3 constructor public Button (Context context) public Button (Context context, AttributeSet attrs) public Button (Context context, AttributeSet attrs, int defStyle) – K_Anas May 10 '12 at 06:53
  • so what's the solution sorry instead of using Button myButton = new Button(this); – AMH May 10 '12 at 06:54
  • my code gives you error? sorry but i can't understand what you really want could you explain more – K_Anas May 10 '12 at 06:57
  • This should be using `setLayoutParams()` rather than `setLayoutParameters()`. – petrus-jvrensburg Feb 13 '16 at 05:58
  • Android Studio 2.3.3 : setLayoutParameters changed to setLayoutParams, LinearLayout.LayoutParams.FILL_PARENT changed to LinearLayout.LayoutParams.MATCH_PARENT – Saravanan Sachi Jun 25 '17 at 13:30
2

Add andoird:id="@+id/linerlayout_1" after that you can access this view easy inside the code with the findviewbyid() method.

For examlpe: place this inside the button's onclicklistener method.

LinearLayout ll = (LinearLayout) findViewById(R.id.linerlayout);
ll.setVisibility(View.GONE); // this row will hide the entire linerlayout
ll.addView(someView); //this row will add the specified View f.e.: TextView
ll.removeView(otherView); // this row will remove the view

And you can manage th view visibility by xml attribute android:visibility too.

kameny
  • 2,372
  • 4
  • 30
  • 40
  • In my opinion there are two way: the first is if you add id for every view and manage the visibility (or the remove) separatly of all view. Or the second way if you manage only the top level layout such as linerarlayout, beacause if you delete the linearlayout the system automatically deletes the child views too. – kameny May 09 '12 at 12:39
1

Look at the setVisibility method of the View class. It allows you to make Views appear or disappear from the UI very simply.

In your button's click listener just add view.setVisibility(View.GONE) to make any layout or widget go away. You can make it reappear by calling view.setVisibility(View.VISIBLE)

Ika
  • 1,608
  • 14
  • 15
0

Sample for dynamically add or remove a view:

TextView tv = new TextView(this);

        tv.setWidth(LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.RIGHT;
        tv.setLayoutParams(params);
        tv.setTextAppearance(this, R.style.darkTextNormal);
        tv.setBackgroundResource(R.color.lightBlue);
        tv.setTextSize(16);

yourLinearLayout.addView(tv);

// or

yourLinearLayout.removeView(tv);
Bob
  • 22,810
  • 38
  • 143
  • 225