Android Expand/collapse animation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Expand and collapse animations are common in Android for FAQs, filter panels, settings groups, and any screen where content should appear progressively instead of all at once. The implementation usually comes down to animating height or letting the transition framework animate a visibility change for you.
Animate Height with ValueAnimator
If you need tight control, measure the target height and animate the layout parameter between collapsed and expanded states.
This works well when you need a real sliding motion instead of a simple visibility toggle.
Use TransitionManager for Simpler Cases
If the animation does not need custom height logic, Android's transition framework can do much of the work for you.
This is often enough for settings screens and simple expandable sections. The tradeoff is less precise control over the animation details.
Measure the Right Height
One of the trickiest parts of manual expand animation is measurement. If the view has not been measured yet, measuredHeight may be zero. That is why expansion code often forces a measure pass before the animation begins.
The exact measurement strategy can differ depending on parent layout constraints, but the principle is the same: do not animate toward an unknown height.
For content whose height changes frequently, you may want to recalculate the target height each time instead of caching it permanently.
Think About UX, Not Just Motion
An expand animation should reveal information, not slow users down. Keep durations short, and avoid animating many heavy nested layouts at once. If the content appears instantly but the height animates smoothly, the UI usually feels responsive without being flashy.
Also consider accessibility and state clarity. An expandable section often benefits from an indicator such as a chevron that changes direction, plus content descriptions that reflect whether the section is expanded or collapsed.
RecyclerView and Large Lists
If you are animating expandable rows inside a RecyclerView, state management becomes more important than the animation itself. Expansion state should live in your data or view-model layer, not only in recycled view instances. Otherwise rows can reopen or collapse unexpectedly while scrolling.
That is a common place where a correct animation function still leads to incorrect UI behavior.
Common Pitfalls
Animating to a height of zero because the view was never measured is a common implementation bug. Measure before animating.
Using expensive nested layouts can make expand and collapse animations stutter on lower-end devices. Keep the animated subtree lightweight.
Storing expansion state only in the view instead of in the backing data causes incorrect behavior in recycled lists.
Summary
- Use
ValueAnimatorwhen you need explicit height control. - Use
TransitionManagerfor simple visibility-based expand and collapse behavior. - Measure the target view correctly before animating.
- Keep animation short and pair it with reliable state management, especially in lists.

