Delete layout programmatically

I have the following xml activity structure of my application. Now I would like to remove the child RelativeLayout programmatically with id layer1Front. How do I do this in code. I do not want to hide it, I need to remove it due to memory problems in my application. Also, after deleting it somehow my application will be easier and faster than the 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> 
+6
source share
2 answers

It would be easiest

 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 application will not be easier or faster after its removal.

+29
source

Try to select the parent layout and remove the child

 parentView.remove(child) 

Hope this works.

+2
source

Source: https://habr.com/ru/post/913538/


All Articles