Android
View.GONE
View.INVISIBLE
UI development
Android programming

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:

kotlin
view.visibility = View.VISIBLE    // Drawn on screen, occupies space
view.visibility = View.INVISIBLE  // Not drawn, but still occupies space
view.visibility = View.GONE       // Not drawn, does not occupy space

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().

kotlin
val loadingIndicator: ProgressBar = findViewById(R.id.loading)
loadingIndicator.visibility = View.INVISIBLE
// The space is still reserved. Surrounding views do not move.

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.

xml
1<LinearLayout
2    android:layout_width="match_parent"
3    android:layout_height="wrap_content"
4    android:orientation="vertical">
5
6    <EditText
7        android:layout_width="match_parent"
8        android:layout_height="wrap_content"
9        android:hint="Enter email" />
10
11    <TextView
12        android:id="@+id/error_label"
13        android:layout_width="match_parent"
14        android:layout_height="wrap_content"
15        android:text="Invalid email format"
16        android:visibility="invisible" />
17
18    <Button
19        android:layout_width="match_parent"
20        android:layout_height="wrap_content"
21        android:text="Submit" />
22
23</LinearLayout>

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.

kotlin
val promoCard: CardView = findViewById(R.id.promo_card)
promoCard.visibility = View.GONE
// The space is reclaimed. Views below shift upward.

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.

kotlin
1fun showPromoIfEligible(user: User) {
2    val promoCard: CardView = findViewById(R.id.promo_card)
3    if (user.isPromoEligible) {
4        promoCard.visibility = View.VISIBLE
5        promoCard.findViewById<TextView>(R.id.promo_text).text = user.promoMessage
6    } else {
7        promoCard.visibility = View.GONE
8    }
9}

VISIBLE, INVISIBLE and GONE compared

BehaviorView.VISIBLEView.INVISIBLEView.GONE
Drawn on screenYesNoNo
Occupies layout spaceYesYesNo
Participates in onMeasure()YesYesNo
Triggers layout pass on toggleNo (already laid out)NoYes
Receives touch eventsYesNoNo
Accessible to screen readersYesNoNo
findViewById() returns itYesYesYes

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.

kotlin
1// Efficient: no layout recalculation
2blinkingDot.visibility = if (isOn) View.VISIBLE else View.INVISIBLE
3
4// Expensive: triggers layout pass every toggle
5expandableSection.visibility = if (expanded) View.VISIBLE else View.GONE

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.

xml
1<!-- In ConstraintLayout, view_b is still anchored to view_a's position,
2     even when view_a is GONE. view_a just collapses to 0x0 size. -->
3<TextView
4    android:id="@+id/view_a"
5    android:visibility="gone"
6    app:layout_constraintStart_toStartOf="parent"
7    app:layout_constraintTop_toTopOf="parent" />
8
9<TextView
10    android:id="@+id/view_b"
11    app:layout_constraintStart_toEndOf="@id/view_a"
12    app:layout_constraintTop_toTopOf="@id/view_a" />

Animating visibility changes

Visibility state matters when you animate a view in or out:

kotlin
1// Fade out and then remove space
2view.animate()
3    .alpha(0f)
4    .setDuration(300)
5    .withEndAction {
6        view.visibility = View.GONE
7    }
8    .start()
9
10// Fade in from invisible (space was already reserved)
11view.alpha = 0f
12view.visibility = View.VISIBLE
13view.animate()
14    .alpha(1f)
15    .setDuration(300)
16    .start()

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.

kotlin
1// Smooth: reserve space first, then animate
2view.visibility = View.INVISIBLE
3view.alpha = 0f
4// ... later when ready to show
5view.visibility = View.VISIBLE
6view.animate().alpha(1f).setDuration(300).start()

Data Binding and Jetpack Compose

With data binding, visibility can be set declaratively:

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="@{viewModel.errorMessage}"
5    android:visibility="@{viewModel.hasError ? View.VISIBLE : View.GONE}" />

Jetpack Compose has no direct equivalent of INVISIBLE. Use conditional composition for GONE behavior and Modifier.alpha() for INVISIBLE behavior:

kotlin
1// Equivalent of GONE: don't compose the element at all
2if (showBanner) {
3    BannerCard(message = promoText)
4}
5
6// Equivalent of INVISIBLE: compose but make transparent
7Box(modifier = Modifier.alpha(if (showLabel) 1f else 0f)) {
8    Text("Status label")
9}

Other gotchas

  • Touches on INVISIBLE views. 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. Calling getWidth() or getHeight() immediately after setting GONE returns stale values because the view has not been remeasured. Use a ViewTreeObserver.OnGlobalLayoutListener or post {} to read dimensions after the layout pass.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms