Ayaz Alavi
Ayaz Alavi

Reputation: 4835

Remove layout programmatically

I have got following xml structure of my app activity. Now I would like to remove child RelativeLayout programmatically with id layer1Front. How would I do that in code. I dont want to hide it, I need to remove it because of memory issues in my app. Also after removing it somehow will my app be lighter and faster than current one?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/layer1Front" >
    </RelativeLayout>   
    <HorizontalScrollView android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <FrameLayout android:layout_width="wrap_content"
            android:layout_height="fill_parent"                   
            android:id="@+id/parallaxLayers"        
            android:visibility="gone">      
        </FrameLayout>
    </HorizontalScrollView>
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/frontView">

    </RelativeLayout>       

</RelativeLayout>

Upvotes: 7

Views: 25401

Answers (2)

Alexander
Alexander

Reputation: 48262

Simplest would be

findViewById(R.id.layer1front).setVisibility(View.GONE);

But then you can also have something like

View root = findViewById(R.id.your_root);
root.removeView(yourViewToRemove);

No, your app is not going to be lighter or faster after removing it

Upvotes: 32

ValayPatel
ValayPatel

Reputation: 1094

Try fetching parent layout and than remove child

parentView.remove(child) 

I hope this works.

Upvotes: 2

Related Questions