Android Development
User Interface
Animation Techniques
App Design
Programming Tips

Android adding simple animations while setvisibility(view.Gone)

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Calling setVisibility(View.GONE) removes a view from layout immediately, which can feel abrupt if the UI changes suddenly. A simple fix is to animate a property such as alpha or translation first, then switch the view to GONE only after the animation finishes.

Fade Out Before Setting GONE

A small fade-out animation is the simplest pattern. The important detail is that setVisibility(View.GONE) should happen in the animation end callback, not before.

kotlin
1import android.animation.Animator
2import android.animation.AnimatorListenerAdapter
3import android.view.View
4
5fun hideWithFade(view: View) {
6    view.animate()
7        .alpha(0f)
8        .setDuration(200)
9        .setListener(object : AnimatorListenerAdapter() {
10            override fun onAnimationEnd(animation: Animator) {
11                view.visibility = View.GONE
12                view.alpha = 1f
13                view.animate().setListener(null)
14            }
15        })
16        .start()
17}

Resetting alpha to 1f after the animation matters so the view is ready for the next time it becomes visible.

Expand or Reappear Cleanly

If you hide with animation, show with animation too. Otherwise the UI can feel inconsistent.

kotlin
1import android.view.View
2
3fun showWithFade(view: View) {
4    view.alpha = 0f
5    view.visibility = View.VISIBLE
6    view.animate()
7        .alpha(1f)
8        .setDuration(200)
9        .setListener(null)
10        .start()
11}

This pattern works well for lightweight panels, validation messages, and secondary controls.

Use Translation for Better Motion

A fade alone is sometimes enough, but adding a small translation can make the motion clearer.

kotlin
1fun hideWithSlide(view: View) {
2    view.animate()
3        .alpha(0f)
4        .translationY(-view.height * 0.1f)
5        .setDuration(200)
6        .withEndAction {
7            view.visibility = View.GONE
8            view.alpha = 1f
9            view.translationY = 0f
10        }
11        .start()
12}

The reset at the end is just as important here as with alpha. Otherwise the next time the view appears, it may still be offset.

When Layout Changes Matter

Sometimes the goal is not just to hide a view visually, but to animate the layout reflow of surrounding views. In those cases, TransitionManager.beginDelayedTransition(...) can be a better fit because it animates the layout change caused by switching between VISIBLE and GONE.

kotlin
1import android.transition.AutoTransition
2import android.transition.TransitionManager
3import android.view.View
4import android.view.ViewGroup
5
6fun toggleSection(container: ViewGroup, section: View) {
7    TransitionManager.beginDelayedTransition(container, AutoTransition())
8    section.visibility = if (section.visibility == View.VISIBLE) View.GONE else View.VISIBLE
9}

This is often the easiest way to animate simple panel expansion or collapse in a form or settings screen.

Keep the Animation Simple

The best visibility animations are usually short and unobtrusive. A 150 to 250 millisecond duration often feels responsive. Long animations make the app feel slower rather than smoother.

Also avoid stacking several animation systems at once. If you animate alpha manually and also let a transition framework animate the same view, the result can become jittery or hard to reason about.

Remember that INVISIBLE and GONE are not interchangeable. INVISIBLE hides the view but keeps its layout space, while GONE removes it from layout. If surrounding views should slide into the empty space, the final state must really be GONE.

In list-heavy screens, also remember that recycled views can retain alpha or translation if the reset logic is incomplete. Visual state cleanup is just as important as the hide animation itself.

Common Pitfalls

Calling setVisibility(View.GONE) before the animation starts makes the view disappear immediately, which defeats the animation.

Forgetting to reset properties such as alpha or translationY causes the next show animation to start from a stale state.

Using complex animations for small UI state changes can hurt perceived responsiveness instead of improving it.

Summary

  • Animate first, then switch to View.GONE in the completion callback.
  • Reset animated properties so the view is ready for the next show cycle.
  • Use TransitionManager when the surrounding layout should animate along with the visibility change.
  • Keep visibility animations short and simple.

Course illustration
Course illustration

All Rights Reserved.