Android adding simple animations while setvisibilityview.Gone
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
View.GONE removes a view from layout, but it does not animate anything by itself. If you call setVisibility(View.GONE) immediately, the view disappears before the user sees a transition, so the usual pattern is to animate properties such as alpha or translationY first and set GONE only when the animation ends.
Why GONE Feels Abrupt
Android layout visibility has three common states:
- '
VISIBLE: draw the view and keep its layout space' - '
INVISIBLE: hide the view but keep its layout space' - '
GONE: hide the view and remove its layout space'
That last behavior is why GONE is useful for collapsing panels, banners, and optional form sections. It is also why a direct visibility change can feel harsh. The layout updates immediately and neighboring views reflow with no visual bridge.
The fix is not to animate GONE itself. The fix is to animate the properties that the user can see, then switch visibility at the end.
A Practical Pattern with ViewPropertyAnimator
For simple show and hide transitions, ViewPropertyAnimator is usually enough. It is lightweight, readable, and built for common UI property changes.
This pattern handles both directions correctly:
- when hiding, animate first and apply
GONEinwithEndAction - when showing, set initial visual state, switch to
VISIBLE, then animate into place
That is the core rule. If you set GONE before the animation, there is nothing left to animate.
When Parent Layout Animation Helps
If the main goal is to animate the layout reflow when children appear or disappear, it may be better to animate the container instead of each child manually. A LayoutTransition on a ViewGroup can animate changes when children are added, removed, or have visibility changes.
Manual animation is usually better when you want precise control over one panel. Container layout animation is better when multiple child views shift around and you want the movement of neighboring views to feel smoother.
Choosing the Right Effect
For a simple hide action, the safest choices are:
- '
alphafor fade-out' - '
translationYortranslationXfor a small slide' - a combination of both for a more polished effect
Avoid large motion for basic toggles. These interactions happen often, so the animation should communicate state change without making the interface feel heavy.
A short duration such as 150 to 250 milliseconds is usually enough. Longer durations may look decorative but can make the app feel slow.
XML Animation Versus Property Animation
Older Android code often uses view animations loaded from res/anim. Those can still work for basic effects, but property animation is generally easier for visibility toggles because it lets you update alpha, translation, and end actions in one place.
The distinction matters because older view animation APIs can change how something looks without actually updating the underlying property values. For visibility transitions tied to layout state, ViewPropertyAnimator is usually the cleaner choice.
Common Pitfalls
The most common mistake is setting View.GONE before the hide animation starts. That removes the view from layout immediately, so the user never sees the effect.
Another mistake is forgetting to reset alpha or translation after hiding. Then the next time the view becomes visible, it may stay transparent or offset.
A third issue is trying to use the same logic for INVISIBLE and GONE without considering layout. GONE changes layout flow; INVISIBLE does not. Choose the one that matches the behavior you want.
Summary
- '
View.GONEdoes not animate on its own.' - Animate visible properties first, then set
GONEin the animation end callback. - Use
ViewPropertyAnimatorfor simple fade and slide transitions. - Reset
alphaand translation when bringing the view back. - Use parent layout animation only when the main concern is child reflow, not just one view fading out.

