Android Difference between View.GONE and View.INVISIBLE?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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. This distinction affects layout performance, animation behavior, and user interaction handling.
The Three Visibility Constants
Android defines three visibility states on every View:
Internally, these map to integer constants (0, 4, and 8 respectively), and they control two independent concerns: whether the view is drawn and whether it participates in layout measurement.
How View.INVISIBLE Works
When you set a view to INVISIBLE, the view still goes through the onMeasure() and onLayout() passes. It occupies the same width and height in its parent container. It simply skips the onDraw() call.
This is useful when you need to toggle a view on and off without causing the rest of the UI to jump around. A common example is a status label that appears and disappears while the form layout stays stable.
The button stays in the same position regardless of whether the error label is visible or invisible.
How View.GONE Works
When you set a view to GONE, it is excluded from layout measurement entirely. The parent layout acts as if the view does not exist, and sibling views fill the space.
This is the correct choice when a UI section is conditionally present, such as a promotional banner that only appears for certain users, or an optional detail section that loads asynchronously.
Comparison Table
| 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 |
A key point: findViewById() returns the view regardless of visibility state. The view object exists in memory in all three cases. Only its participation in layout and drawing changes.
Layout Performance Implications
Toggling between VISIBLE and GONE triggers a full layout pass because the parent needs to remeasure and reposition its children. Toggling between VISIBLE and INVISIBLE does not trigger a layout pass because the space allocation remains unchanged. It only triggers an invalidation (redraw).
For views that toggle frequently (multiple times per second, like in an animation loop), INVISIBLE is significantly cheaper than GONE.
In ConstraintLayout, views set to GONE have special behavior: their constraints are still respected for positioning other views, but their dimensions collapse to zero. This is different from LinearLayout or RelativeLayout, where GONE views are simply removed from the layout calculation.
Animations and Visibility
When animating a view's appearance or disappearance, visibility state matters:
If you use GONE and then animate to VISIBLE, there will be a visible layout jump as sibling views shift to make room. To avoid this, set the view to INVISIBLE first, then animate it to full alpha. The space is already reserved, so the layout does not shift.
Interaction with Data Binding and Compose
In data binding, visibility can be controlled declaratively:
In Jetpack Compose, the concept is handled differently. There is no direct equivalent of INVISIBLE. Instead, you control visibility with conditional composition and the Modifier.alpha() modifier:
Common Pitfalls
- Using
View.GONEfor views that toggle rapidly (like blinking indicators). Each toggle triggers a layout pass, causing unnecessary CPU work and potential jank. - Assuming
View.INVISIBLEviews cannot intercept touches. While they do not receive standard click events, custom touch handlers on the parent can still hit-test against invisible children depending on implementation. - Forgetting that
ConstraintLayouttreatsGONEviews differently from other layouts. Constraints to aGONEview are preserved but resolve to the view's position with zero dimensions. - Setting a view to
GONEand then immediately callinggetWidth()orgetHeight(). The view has not been remeasured yet. Use aViewTreeObserver.OnGlobalLayoutListenerorpost {}to read dimensions after the layout pass. - Animating alpha to 0 but forgetting to set visibility afterward. The view is still
VISIBLEwith alpha 0, so it still intercepts touch events and occupies space. - Using
INVISIBLEwhenGONEis correct, leaving unexplained blank gaps in the UI that confuse users.
Summary
View.INVISIBLEhides a view but keeps its space. Use it when the layout should remain stable during visibility toggles.View.GONEhides a view and releases its space. Use it when the view is conditionally present and other content should fill the gap.INVISIBLEis cheaper to toggle because it skips layout recalculation.GONEtriggers a full layout pass.- In
ConstraintLayout,GONEviews collapse to zero size but their constraints remain active. - For animations, use
INVISIBLEto reserve space before fading in, preventing layout jumps. - In Jetpack Compose, use conditional composition for
GONEbehavior andModifier.alpha()forINVISIBLEbehavior.
Related reading
- Android disabling highlight on listView click
- Android Drawing Separator/Divider Line in Layout?
- Android Eclipse - Could not find .apk
- 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
.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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.