Android : Difference between View.GONE and View.INVISIBLE?
Asked Answered
H

8

551

What is the difference between View.INVISIBLE and View.GONE for the View visibility status?

Hurtado answered 19/7, 2012 at 8:14 Comment(1)
When a View is gone, it means it doesn't take any space in the layout. When it is invisible, it will take the necessary room in a layout but you just don't see it.Subscribe
S
834

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.

Saltworks answered 19/7, 2012 at 8:15 Comment(7)
Looks like analogous to display:none & visibility:hidden in HTML/CSS :-)Performance
Or to Hidden and Collapsed in Wpf XAMLIntone
If it is invisible, will click on it is also disabled?Persis
@KuldeepYadav yes it will be in both gone and invisible.Carbone
just for future curious readers: we can still use performItemClick on list/grid views even it is invisible.Grout
when i use gone the touch doesn't work at that place.Barbaraanne
Note that INVISIBLE still makes the view clickable; it is the same as VISIBLE but without being shown.Haletta
C
317

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

enter image description here

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

enter image description here

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

enter image description here

Hope this will clear your doubts.

Crackling answered 22/4, 2014 at 5:25 Comment(1)
what if i use it for edittext. is it possible to get the value from edittext ?Metastasize
A
35

For ListView or GridView there is an another difference, when visibility initially set to

INVISIBLE:

Adapter's getView() function called

GONE:

Adapter's getView() function didn't call, thus preventing views to load, when it is unnecessary

Anacoluthon answered 26/3, 2014 at 9:6 Comment(0)
T
10

INVISIBLE:
The view has to be drawn and it takes time.

GONE:
The view doesn't have to be drawn.

Tachometer answered 26/10, 2015 at 7:49 Comment(2)
View is not drawn, it is measured and laid out.Alamo
@Alamo maybe he/she means ondraw method!Gadget
H
10

I'd like to add to the right and successful answers, that if you initialize a view with visibility as View.GONE, the view could have been not initialized and you will get some random errors.

For example if you initialize a layout as View.GONE and then you try to start an animation, from my experience I've got my animation working randomly times. Sometimes yes, sometimes no.

So before handling (resizing, move, whatever) a view, you have to init it as View.VISIBLE or View.INVISIBLE to render it (draw it) in the screen, and then handle it.

Historied answered 10/11, 2015 at 12:39 Comment(1)
Yes, you are right, It working only sometimes when view is GONEValdis
I
4

when you make it Gone every time of compilation of the program the component gets initialized which means you are removing the component from the layout and when you make it invisible the component it will take the same space in the layout but every time you don't need to initialize it.

if you set Visibility=Gone then you have to initialize the component..like

eg

Button _mButton = new Button(this);
_mButton = (Button)findViewByid(R.id.mButton);

so it will take more time as compared to Visibility = invisible.

Ingressive answered 21/7, 2016 at 5:17 Comment(0)
C
4
  • View.INVISIBLE->The View is invisible but it will occupy some space in layout

  • View.GONE->The View is not visible and it will not occupy any space in layout

Concinnity answered 10/3, 2017 at 11:20 Comment(0)
P
1

View.GONE - The view will not show and the rest of the views will not take its existence into consideration.

View.INVISIBLE - The view will not show, but it will take its assigned space in the layout.

Pyrrhuloxia answered 4/2, 2020 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.