android – Removing a View from an Activity-ThrowExceptions

Exception or error:

You’ve got a View defined in a layout file. How do you remove it from you Activity code?

How to solve:
ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);

should do what you want as far as correctly removing the View from the Activity. The other guys’ answer will just make the View invisible, using up resources.

###

As Android mentioned in a comment:

view.setVisibility(View.GONE);

Quoting the Android reference:

This view is invisible, and it doesn’t take any space for layout purposes.

I think it’s the best solution unless you really need to delete the object.

Leave a Reply

Your email address will not be published. Required fields are marked *