Android: difference between View.GONE and View.INVISIBLE
Codemia Knowledge Hub ·
View.GONE removes the view from the layout entirely, so other views fill the vacated space. View.INVISIBLE hides the view visually but keeps its space reserved, so the rest of the layout does not shift. The choice also affects layout performance, animations and touch handling.
The three visibility constants
Every View has three visibility states:
They map to the integer constants 0, 4 and 8, and control two separate things: whether the view is drawn and whether it takes part in layout measurement.
How View.INVISIBLE works
An INVISIBLE view still goes through onMeasure() and onLayout() and keeps its width and height in the parent. It just skips onDraw().
Use it to toggle a view without making the rest of the UI jump, for example a status label that appears and disappears while the form stays put.
The button stays in place whether the error label is visible or invisible.
How View.GONE works
A GONE view is left out of layout measurement entirely. The parent acts as if it does not exist, and siblings fill the space.
Use it when a section is only conditionally present, such as a promo banner shown to certain users or an optional detail section that loads asynchronously. Using INVISIBLE here instead would leave unexplained blank gaps.
VISIBLE, INVISIBLE and GONE compared
| Behavior | View.VISIBLE | View.INVISIBLE | View.GONE |
| Drawn on screen | Yes | No | No |
| Occupies layout space | Yes | Yes | No |
Participates in onMeasure() | Yes | Yes | No |
| Triggers layout pass on toggle | No (already laid out) | No | Yes |
| Receives touch events | Yes | No | No |
| Accessible to screen readers | Yes | No | No |
findViewById() returns it | Yes | Yes | Yes |
findViewById() returns the view in every state. The object exists in memory in all three cases; only its part in layout and drawing changes.
Layout performance
Toggling between VISIBLE and GONE triggers a full layout pass, because the parent must remeasure and reposition its children. Toggling between VISIBLE and INVISIBLE leaves the space allocation unchanged, so it only triggers an invalidation (redraw).
For views that toggle often (several times per second, as in an animation loop or a blinking indicator), INVISIBLE is much cheaper. Using GONE there causes unnecessary CPU work and possible jank.
GONE in ConstraintLayout
In ConstraintLayout, a GONE view's constraints still position other views, but its dimensions collapse to zero. In LinearLayout or RelativeLayout, GONE views are simply removed from the layout calculation.
Animating visibility changes
Visibility state matters when you animate a view in or out:
If you fade alpha to 0, remember to set the visibility afterwards. Otherwise the view stays VISIBLE at alpha 0, still intercepting touches and taking up space.
Going from GONE straight to VISIBLE causes a visible jump as siblings shift to make room. Set the view to INVISIBLE first, then animate it to full alpha; the space is already reserved, so nothing shifts.
Data Binding and Jetpack Compose
With data binding, visibility can be set declaratively:
Jetpack Compose has no direct equivalent of INVISIBLE. Use conditional composition for GONE behavior and Modifier.alpha() for INVISIBLE behavior:
Other gotchas
- Touches on
INVISIBLEviews. They do not receive standard click events, but custom touch handlers on the parent can still hit-test against invisible children, depending on the implementation. - Reading size right after
GONE. CallinggetWidth()orgetHeight()immediately after settingGONEreturns stale values because the view has not been remeasured. Use aViewTreeObserver.OnGlobalLayoutListenerorpost {}to read dimensions after the layout pass.
Related reading
- Android disabling highlight on listView click
- Android Drawing Separator/Divider Line in Layout?
- Android Eclipse - Could not find .apk
- Android EditText deletebackspace key event
- Android ektorp couchbase replication error
- Android elevation not showing a shadow
- Android emulator-5554 offline
- Android emulator doesn't take keyboard input - SDK tools rev 20
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.