State of Compose 2023 results are in! Click here to learn more

← Back to Compose Animation

AnimatedVisibility

Component
in
Compose Animation
. Since 0.1.0-dev17

Overview

Code Examples

Video

AnimatedVisibility composable animates the appearance and disappearance of its content, as visible value changes. Different EnterTransitions and ExitTransitions can be defined in enter and exit for the appearance and disappearance animation. There are 4 types of EnterTransition and ExitTransition: Fade, Expand/Shrink, Scale and Slide. The enter transitions can be combined using +. Same for exit transitions. The order of the combination does not matter, as the transition animations will start simultaneously. See EnterTransition and ExitTransition for details on the three types of transition.

Aside from these three types of EnterTransition and ExitTransition, AnimatedVisibility also supports custom enter/exit animations. Some use cases may benefit from custom enter/exit animations on shape, scale, color, etc. Custom enter/exit animations can be created using the Transition{'<'}EnterExitState{'>'} object from the AnimatedVisibilityScope (i.e. AnimatedVisibilityScope.transition). See the second sample code snippet below for example. These custom animations will be running alongside of the built-in animations specified in enter and exit. In cases where the enter/exit animation needs to be completely customized, enter and/or exit can be specified as EnterTransition.None and/or ExitTransition.None as needed. AnimatedVisibility will wait untilall* of enter/exit animations to finish before it considers itself idle. content will only be removed after all the (built-in and custom) exit animations have finished.

AnimatedVisibility creates a custom Layout for its content. The size of the custom layout is determined by the largest width and largest height of the children. All children will be aligned to the top start of the Layout.

Note: Once the exit transition is finished, the content composable will be removed from the tree, and disposed. If there's a need to observe the state change of the enter/exit transition and follow up additional action (e.g. remove data, sequential animation, etc), consider the AnimatedVisibility API variant that takes a MutableTransitionState parameter.

By default, the enter transition will be a combination of fadeIn and expandIn of the content from the bottom end. And the exit transition will be shrinking the content towards the bottom end while fading out (i.e. fadeOut + shrinkOut). The expanding and shrinking will likely also animate the parent and siblings if they rely on the size of appearing/disappearing content. When the AnimatedVisibility composable is put in a Row or a Column, the default enter and exit transitions are tailored to that particular container. See RowScope.AnimatedVisibility and ColumnScope.AnimatedVisibility for details.

Here are two examples of AnimatedVisibility: one using the built-in enter/exit transition, the other using a custom enter/exit animation.

Overloads

AnimatedVisibility

@Composable
fun AnimatedVisibility(
    visible: Boolean,
    modifier: Modifier = Modifier,
    enter: EnterTransition = fadeIn() + expandIn(),
    exit: ExitTransition = shrinkOut() + fadeOut(),
    label: String = "AnimatedVisibility",
    content: @Composable() AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibledefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking by default
contentContent to appear or disappear based on the value of visible @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibilityScop

AnimatedVisibility

@Composable
fun RowScope.AnimatedVisibility(
    visible: Boolean,
    modifier: Modifier = Modifier,
    enter: EnterTransition = fadeIn() + expandHorizontally(),
    exit: ExitTransition = fadeOut() + shrinkHorizontally(),
    label: String = "AnimatedVisibility",
    content: @Composable() AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibledefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding horizontally by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking horizontally by default
contentContent to appear or disappear based on the value of visible @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibility @see ColumnScope.AnimatedVisibility @see AnimatedVisibilityScop

AnimatedVisibility

@Composable
fun ColumnScope.AnimatedVisibility(
    visible: Boolean,
    modifier: Modifier = Modifier,
    enter: EnterTransition = fadeIn() + expandVertically(),
    exit: ExitTransition = fadeOut() + shrinkVertically(),
    label: String = "AnimatedVisibility",
    content: @Composable AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibledefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding vertically by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking vertically by default
contentContent to appear or disappear based on the value of visible @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibility @see AnimatedVisibilityScop

AnimatedVisibility

@Composable
fun AnimatedVisibility(
    visibleState: MutableTransitionState<Boolean>,
    modifier: Modifier = Modifier,
    enter: EnterTransition = fadeIn() + expandIn(),
    exit: ExitTransition = fadeOut() + shrinkOut(),
    label: String = "AnimatedVisibility",
    content: @Composable() AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibleStatedefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking by default
contentContent to appear or disappear based on the value of visibleState @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibility @see Transition.AnimatedVisibility @see AnimatedVisibilityScop

AnimatedVisibility

@Composable
fun RowScope.AnimatedVisibility(
    visibleState: MutableTransitionState<Boolean>,
    modifier: Modifier = Modifier,
    enter: EnterTransition = expandHorizontally() + fadeIn(),
    exit: ExitTransition = shrinkHorizontally() + fadeOut(),
    label: String = "AnimatedVisibility",
    content: @Composable() AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibleStatedefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding vertically by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking vertically by default
contentContent to appear or disappear based on the value of visibleState @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibility @see Transition.AnimatedVisibility @see AnimatedVisibilityScop

AnimatedVisibility

@Composable
fun ColumnScope.AnimatedVisibility(
    visibleState: MutableTransitionState<Boolean>,
    modifier: Modifier = Modifier,
    enter: EnterTransition = expandVertically() + fadeIn(),
    exit: ExitTransition = shrinkVertically() + fadeOut(),
    label: String = "AnimatedVisibility",
    content: @Composable() AnimatedVisibilityScope.() -> Unit
)

Parameters

NameDescription
visibleStatedefines whether the content should be visible
modifiermodifier for the Layout created to contain the content
enterEnterTransition(s) used for the appearing animation, fading in while expanding vertically by default
exitExitTransition(s) used for the disappearing animation, fading out while shrinking vertically by default
contentContent to appear or disappear based on of visibleState @see EnterTransition @see ExitTransition @see fadeIn @see expandIn @see fadeOut @see shrinkOut @see AnimatedVisibility @see Transition.AnimatedVisibility @see AnimatedVisibilityScop