Dynamically add delete linearlayout control

I have 3 layouts, I need it when you click on the button to access a specific layout and remove ads from controls and in it any idea how to achieve this is the code that 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>
+4
source share
4 answers

Delete image from parent on Android:

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

Android removes all child views:

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

Add to parents 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
+20
source

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

For example: put this inside the onclicklistener button 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

xml android:visibility.

+2

Take a look at the setVisibility method of the View class. This allows you to very easily display or disappear from the user interface.

In your click listener button, simply add view.setVisibility(View.GONE)to remove any layout or widget. You can do it again by callingview.setVisibility(View.VISIBLE)

+1
source

Example for dynamically adding or removing views:

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);
0
source

All Articles