sharedElement / sharedBounds
The same key in both branches.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Moves the same element smoothly from one layout to another, so users can follow it between two states of the screen.
Good for
- A thumbnail growing into a detail view
- Expanding a compact card
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteStateLayout
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.sharedBounds
import androidx.compose.remote.creation.compose.modifier.sharedElement
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.remoteTween
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun SharedElement(durationMillis: Int, bounds: Boolean) {
val expanded = remember { MutableRemoteBoolean(false) }
// The same key in both states: the player moves the element between them.
val shared =
if (bounds) RemoteModifier.sharedBounds(1, remoteTween(durationMillis))
else RemoteModifier.sharedElement(1, remoteTween(durationMillis))
RemoteStateLayout(expanded, RemoteModifier.clickable(valueChangeAction(expanded, !expanded))) {
isExpanded ->
if (isExpanded) {
RemoteBox(
RemoteModifier.size(300.rdp, 220.rdp),
contentAlignment = RemoteAlignment.BottomEnd,
) {
RemoteBox(
shared.size(200.rdp, 120.rdp).background(Color(0xFF1E88E5).rc),
contentAlignment = RemoteAlignment.Center,
) {
RemoteText("Tap to collapse".rs, color = Color.White.rc)
}
}
} else {
RemoteBox(
RemoteModifier.size(300.rdp, 220.rdp),
contentAlignment = RemoteAlignment.TopStart,
) {
RemoteBox(
shared.size(90.rdp).background(Color(0xFF1E88E5).rc),
contentAlignment = RemoteAlignment.Center,
) {
RemoteText("Tap".rs, color = Color.White.rc)
}
}
}
}
}
API
creation.compose.modifier.sharedElement
RemoteModifier sharedElement(RemoteModifier, int key, optional RemoteTweenSpec spec, optional RemoteEnterTransition enter, optional RemoteExitTransition exit)creation.compose.modifier.sharedBounds
RemoteModifier sharedBounds(RemoteModifier, int key, optional RemoteTweenSpec spec, optional RemoteEnterTransition enter, optional RemoteExitTransition exit)