Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a layout that was added using addContentView()?

Tags:

android

I am adding a layout using addContentView(). How can i remove this layout on a Button click ?

like image 757
James Avatar asked Mar 27 '12 03:03

James


4 Answers

Assuming contentView is the view that was added via window.addContentView()

((ViewGroup) contentView.getParent()).removeView(contentView);
like image 195
Nick Dowell Avatar answered Oct 15 '22 09:10

Nick Dowell


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
    }
}
like image 34
VinceStyling Avatar answered Oct 15 '22 10:10

VinceStyling


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.

like image 5
Frank Avatar answered Oct 15 '22 10:10

Frank


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.

like image 2
zrgiu Avatar answered Oct 15 '22 09:10

zrgiu