Android:Difference Between Invisible and Gone

Android : difference between invisible and gone?

INVISIBLE:

This view is invisible, but it still takes up space for layout purposes.

GONE:

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

what is difference b/w View.GONE and View.INVISIBLE

Read more about Android : difference between invisible and gone?

From Documentation you can say that

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

View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.


Lets clear the idea with some pictures.

Assume that you have three buttons, like below

Sample Image

Now if you set visibility of Button Two as invisible (View.INVISIBLE), then output will be

Sample Image

And when you set visibility of Button Two as gone (View.GONE) then output will be

Sample Image

Hope this will clear your doubts.

Under which circumstance should INVISIBLE used instead of GONE?

You're on the right track thinking about the impact on measuring. Which one is more efficient all depends on how frequently you are changing the view's visibility.

For example, if the view is not visible for a majority of the time, making it GONE would probably be more efficient, because the system would not be needlessly measuring and laying out your invisible view whenever it needs to adjust other views on the screen.

On the other hand, if the view changes between visible and invisible frequently, you might get better performance from INVISIBLE as you would potentially avoid an extra measure/layout on each transition.

GONE View behaves like INVISIBLE (android)

I think you're worrying too much about what's happening behind the scenes. Here's what happens if you add a TextView below that View:

The Layout Inspector showing that the View is still full-size, but isn't affecting the rest of the layout

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!

Difference between removing a view and setting it's visibility as GONE?

If you remove a view from the parent, its no longer in its list of children. You can then add it to any other ViewGroup. If you loop through the old paren't children it won't come up, and it won't be called when the ViewGroup does things like resize itself.

If you make it GONE, its still a child of the ViewGroup. It can't be added to another, because it can have only 1 parent. It will still be called for things like resizing of the view group.

Difference between foo.setVisibility(View.GONE) and parent.removeView(foo)

If you need to remove them and then show them again, it could be better to just set visibility to gone and then change back to visible again.

If you on the other hand don't need them visible again, simply remove them.

Buttons in android : Visible, Invisible, Gone

try the code below

 if (code != null && !code.equels("")
{
startbtn.setVisibility(View.VISIBLE);
}
else
{
startbtn.setVisibility(View.GONE);
}
startbtn.setOnClickListener(new View.OnClickListener() {
//Required action
}


Related Topics



Leave a reply



Submit