๐ฌ Seamless Entry and Exit Animations with AnimatedVisibility in Jetpack Compose
Learn how to animate your composables as they appear and disappear on screen using AnimatedVisibility, and create delightful, smooth UIโฆ
๐ฌ Seamless Entry and Exit Animations with AnimatedVisibility in Jetpack Compose

Learn how to animate your composables as they appear and disappear on screen using AnimatedVisibility, and create delightful, smooth UI transitions effortlessly.
๐ Intro: Visibility That Feels Alive
Imagine this: You toggle a button, and instead of a card suddenly popping in, it slides up gracefully. Or when dismissed, it fades away softly โ natural, fluid, and meaningful.
Thatโs where AnimatedVisibility shines โ itโs the Compose-native way to animate enter and exit transitions of composables based on state.
And unlike older approaches (using View animations or TransitionManager), this one is:
- Declarative ๐งฉ
- Lightweight โก
- Fully tied to Compose state management ๐ช
Today, weโll build a real example โ a โShow More Detailsโ card that expands, fades, and shrinks with smooth motion.
๐ง Concept Overview: How AnimatedVisibility Works
The API is simple yet powerful:
AnimatedVisibility(visible = isVisible) {
// Your composable content
}
When isVisible changes:
- Compose automatically triggers the enter and exit animations.
- You can customize those transitions with slide, expand, fade, or scale effects.
Letโs see it in action ๐
โ๏ธ Implementation: Building the Interactive Animated Card
๐น Step 1: Basic Visibility Toggle
@Composable
fun AnimatedVisibilityDemo() {
var isVisible by remember { mutableStateOf(false) }
Column(
modifier = Modifier
.fillMaxSize()
.padding(24.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = { isVisible = !isVisible }) {
Text(if (isVisible) "Hide Details" else "Show Details")
}
Spacer(modifier = Modifier.height(20.dp))
AnimatedVisibility(visible = isVisible) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(180.dp)
.clip(RoundedCornerShape(16.dp))
.background(Color(0xFF7E57C2)),
contentAlignment = Alignment.Center
) {
Text(
"Compose makes animation easy!",
color = Color.White,
style = MaterialTheme.typography.titleMedium
)
}
}
}
}
โจ Tap the button โ the card appears and disappears with default fade + expand animation.
๐น Step 2: Custom Enter and Exit Animations
Default is nice, but letโs spice it up!
AnimatedVisibility(
visible = isVisible,
enter = fadeIn(animationSpec = tween(500)) +
slideInVertically(initialOffsetY = { it }),
exit = fadeOut(animationSpec = tween(400)) +
slideOutVertically(targetOffsetY = { it / 2 })
) {
Box(
modifier = Modifier
.fillMaxWidth()
.height(180.dp)
.clip(RoundedCornerShape(16.dp))
.background(Color(0xFF26C6DA)),
contentAlignment = Alignment.Center
) {
Text(
"Custom enter/exit animations!",
color = Color.White,
style = MaterialTheme.typography.titleMedium
)
}
}
๐ Now the card:
- Slides in from below
- Fades out partially upward when hidden
This combo feels natural and gives a smooth contextual reveal.
๐น Step 3: Combining Multiple Animated Elements
We can also animate multiple UI parts independently
@Composable
fun MultiElementAnimation() {
var expanded by remember { mutableStateOf(false) }
Column(
modifier = Modifier
.fillMaxSize()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Button(onClick = { expanded = !expanded }) {
Text(if (expanded) "Collapse" else "Expand")
}
AnimatedVisibility(
visible = expanded,
enter = expandVertically() + fadeIn(),
exit = shrinkVertically() + fadeOut()
) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(Color(0xFF42A5F5))
.padding(16.dp)
.clip(RoundedCornerShape(12.dp))
) {
Text("User Profile", color = Color.White, fontSize = 18.sp)
Spacer(Modifier.height(8.dp))
Text("Name: Nishanth", color = Color.White)
Text("Level: Advanced", color = Color.White)
Text("Status: Online", color = Color.White)
}
}
}
}
๐ก The column expands and collapses gracefully. No jarring pops โ just fluid motion.
๐งฉ Step 4: Animating Child Composables Inside
Want even more control? Each element inside AnimatedVisibility can have its own animation using animateEnterExit.
AnimatedVisibility(
visible = expanded,
enter = fadeIn(),
exit = fadeOut()
) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Icon(
imageVector = Icons.Default.Favorite,
contentDescription = null,
modifier = Modifier
.animateEnterExit(
enter = slideInHorizontally(initialOffsetX = { -40 }),
exit = slideOutHorizontally(targetOffsetX = { 40 })
)
.padding(8.dp)
)
Text("Liked!", color = Color.White)
}
}
๐ฏ Each child reacts differently โ giving your animation storytelling depth.
๐ง Pro Tip: Performance & Best Practices
โ Use AnimatedVisibility for state-driven UI, not frequent recompositions โ Keep animations short (300โ600ms) for UX comfort โ Use AnimatedContent for content replacement (next-level topic soon!) โ Remember: animations recompose efficiently, so no performance worry
๐งช Interactive Challenge
Try this yourself:
- Add expandHorizontally() + fadeIn() together
- Add an AnimatedVisibility around a LazyColumn
- Chain LaunchedEffect to start animation after 2 seconds
- Play with different animationSpec types (spring, tween, keyframes)
๐ก Every experiment shows how Compose builds motion declaratively.
๐งฉ Takeaway
AnimatedVisibility transforms how you handle dynamic layouts โ turning conditional rendering into beautiful motion.
โVisibility is not just a state โ itโs an experience.โ
Mastering it gives you the confidence to craft UIs that breathe with state changes.
Before you leave โ -
- ๐ฌ Iโd love to hear from you! What topics would you like me to write about next? Letโs learn and grow together! ๐ฑ
- Follow me for more update on Andorid/kotlin and latest Tech
- Give me a ๐ Clap to put it one step up for others readers
- Connect Me on on LinkedIn!
Thanks for the read!.
๋ฉํ๋ฐ์ดํฐ
- post_id
- dbb38dce4d0c
- slug
- seamless-entry-and-exit-animations-with-animatedvisibility-in-jetpack-compose-dbb38dce4d0c
- url
- https://medium.com/@snishanthdeveloper/seamless-entry-and-exit-animations-with-animatedvisibility-in-jetpack-compose-dbb38dce4d0c
- canonical_url
- https://medium.com/@snishanthdeveloper/seamless-entry-and-exit-animations-with-animatedvisibility-in-jetpack-compose-dbb38dce4d0c
- author_url
- https://medium.com/@snishanthdeveloper
- status
- ok
- fetched_at
- 2026-09-17 10:54:01