Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 806
Ayaz Alavi Avatar asked Apr 18 '12 14:04

Ayaz Alavi


People also ask

Can we add or delete any view type in XML on runtime?

Yes, because you can not add/delete any View Type in XML on runtime. Dynamic layouts are developed using Java and can be used to create layouts that you would normally create using an XML file.


2 Answers

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

like image 162
Alexander Kulyakhtin Avatar answered Sep 19 '22 23:09

Alexander Kulyakhtin


Try fetching parent layout and than remove child

parentView.remove(child) 

I hope this works.

like image 38
ValayPatel Avatar answered Sep 19 '22 23:09

ValayPatel