I am adding a layout using addContentView()
.
How can i remove this layout on a Button click ?
Assuming contentView
is the view that was added via window.addContentView()
((ViewGroup) contentView.getParent()).removeView(contentView);
try it
View youAddedView;
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
for (int i = 0; i < rootView.getChildCount(); i++) {
if(rootView.getChildAt(i) == yourAddedView) {
// do anything here
}
}
If you already have the reference to the view you can simply just do :
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
rootView.removeView(viewToRemove);
Instead of looping through the ViewGroup.
Unfortunately there's no way of removing a content view that was added with addContentView()
. The best you can you do is to call setVisibility(View.GONE)
on it, to hide it.
That is why the activity's onContentChanged() only gets called when the content view is set or added to an activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With