I have set in XML visibility="gone"
for the View. On the first click on the button (), the View should become visible (it works) viewView.visibility = VISIBLE
. The second time you click on the button, the view should become "gone" again viewView.visibility = GONE
.
But instead, the View doesn't disappear, but just becomes transparent, as if I had set invisible
(look at the illustration from Layout Inspector).
How can I make the View truly "gone" like in the first frame?
Yes view
still has its full size, but it's not actually affecting the layout. That's what GONE
means - it's not visible, but it's also behaving as though it's not there, and everything else is laid out accordingly.
That doesn't mean the view itself has been recalculated with zero height or anything - for one thing, removing something doesn't change its height, right? It's just not in the layout anymore, so the space it's taking up is now zero. And there's also no reason to recalculate the height while the View
isn't being displayed, it would be wasted work. It's just there, hanging around, waiting to be added to the layout again.
It's not like you can click it while it's in this state anyway, so what it's actually doing and how the system is managing stuff shouldn't be relevant. If your code does somehow rely on the View
being "gone" in some sense (not present in the layout, garbage collected, zero width and height) then you'll have to account for determining that yourself, and whatever you're doing now isn't enough. You could check its visibility
property remember!
Also just as an aside, be careful about what you name things - an ID called view
and especially a variable called view
inside a class that already has a view
property is just asking for trouble. Same with a variable called isVisible
being referenced in the scope of a Button
and a Fragment
, both of which have a property with the same name (and you'll get a warning about this one, accidental override
)
It just leads to bugs when you accidentally reference the wrong thing. If these are just placeholder names you're using for your question, bear in mind people will copy your code to try and help you, and you're making work for them when they have to fix it. Not having a go or anything, just something to keep in mind!