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.
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.
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.
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.
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.GONEin the completion callback. - Reset animated properties so the view is ready for the next show cycle.
- Use
TransitionManagerwhen the surrounding layout should animate along with the visibility change. - Keep visibility animations short and simple.

