androidx.compose.material3
SwipeToDismissBox
Composable function
A swipe-to-dismiss box lets the user swipe an item sideways, for example to delete or archive it. While swiping, a background shows what will happen.
The box keeps the swipe position in a state. Create it with rememberSwipeToDismissBoxState().

// Swiped open only for this picture. Normally: rememberSwipeToDismissBoxState()
val state = rememberSwipeToDismissBoxState(initialValue = SwipeToDismissBoxValue.EndToStart)
SwipeToDismissBox(
state = state,
backgroundContent = {
Box(
Modifier.fillMaxSize().background(MaterialTheme.colorScheme.errorContainer).padding(horizontal = 20.dp),
contentAlignment = Alignment.CenterEnd,
) { Icon(Icons.Filled.Delete, contentDescription = "Delete") }
},
onDismiss = { direction -> /* remove the item */ },
modifier = Modifier.width(360.dp),
) {
ListItem(headlineContent = { Text("Swipe me") })
}
When the item is swiped all the way, onDismiss runs with the direction. Remove the item from your list there.
Read state.dismissDirection in backgroundContent to show a different background for each direction.
Parameters
@Composable
fun SwipeToDismissBox(
state: SwipeToDismissBoxState,
backgroundContent: @Composable RowScope.() -> Unit,
modifier: Modifier = Modifier,
enableDismissFromStartToEnd: Boolean = true,
enableDismissFromEndToStart: Boolean = true,
gesturesEnabled: Boolean = true,
onDismiss: (SwipeToDismissBoxValue) -> Unit = {},
content: @Composable RowScope.() -> Unit,
)
state: SwipeToDismissBoxStateRequired
state: SwipeToDismissBoxStateRequiredThe swipe position. Create it with rememberSwipeToDismissBoxState().
backgroundContent: @Composable RowScope.() -> UnitRequired
backgroundContent: @Composable RowScope.() -> UnitRequiredWhat shows behind the item while swiping, usually a color and an icon. Placed in a Row.
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position. See the Modifier guide.
enableDismissFromStartToEnd: BooleanOptional
enableDismissFromStartToEnd: BooleanOptionalDefault: true
Set to false to block swiping from the start side.
enableDismissFromEndToStart: BooleanOptional
enableDismissFromEndToStart: BooleanOptionalDefault: true
Set to false to block swiping from the end side.
gesturesEnabled: BooleanOptional
gesturesEnabled: BooleanOptionalDefault: true
Set to false to turn off swiping.
onDismiss: (SwipeToDismissBoxValue) -> UnitOptionalSince 1.9
onDismiss: (SwipeToDismissBoxValue) -> UnitOptionalSince 1.9Default: {}
Called when the item is swiped away. Receives the direction: StartToEnd or EndToStart.
Longer Example
Swipe a row to the start to delete it, with an "Undo" action in a snackbar.
- Each row has its own
rememberSwipeToDismissBoxState(). Thekeyinitemskeeps it with the right row. enableDismissFromStartToEnd = falseallows only one direction.onDismissremoves the item from the list and shows the snackbar. "Undo" puts it back at the same position.Modifier.animateItem()closes the gap smoothly.

val sampleContacts = listOf(
val contacts = remember { sampleContacts.take(4).toMutableStateList() }
val snackbar = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
Scaffold(
snackbarHost = { SnackbarHost(snackbar) },
modifier = Modifier.size(360.dp, 340.dp),
) { padding ->
LazyColumn(contentPadding = padding) {
items(contacts, key = { it.id }) { contact ->
// One state per row. The key above makes sure it stays with its row.
val state = rememberSwipeToDismissBoxState()
SwipeToDismissBox(
state = state,
enableDismissFromStartToEnd = false,
backgroundContent = {
Box(
Modifier.fillMaxSize().background(MaterialTheme.colorScheme.errorContainer).padding(horizontal = 20.dp),
contentAlignment = Alignment.CenterEnd,
) { Icon(Icons.Filled.Delete, contentDescription = "Delete") }
},
onDismiss = {
val index = contacts.indexOf(contact)
contacts.remove(contact)
scope.launch {
val result = snackbar.showSnackbar("${contact.name} deleted", actionLabel = "Undo")
if (result == SnackbarResult.ActionPerformed) contacts.add(index, contact)
}
},
modifier = Modifier.animateItem(),
) {
ListItem(headlineContent = { Text(contact.name) }, leadingContent = { Avatar(contact.name) })
}
}
}
}
/** The data of one row. A data class with a stable id, used as the list key. */
data class Contact(val id: Int, val name: String, val email: String, val favorite: Boolean = false)
private val sampleContacts = listOf(
Contact(1, "Ada Lovelace", "ada@example.com", favorite = true),
Contact(2, "Alan Turing", "alan@example.com"),
Contact(3, "Barbara Liskov", "barbara@example.com"),
Contact(4, "Grace Hopper", "grace@example.com", favorite = true),
Contact(5, "Katherine Johnson", "katherine@example.com"),
Contact(6, "Linus Torvalds", "linus@example.com"),
)
/** A round avatar with the first letter of the name. */
@Composable
private fun Avatar(name: String) {
Box(
Modifier.size(40.dp).background(MaterialTheme.colorScheme.primaryContainer, CircleShape),
contentAlignment = Alignment.Center,
) { Text(name.take(1), color = MaterialTheme.colorScheme.onPrimaryContainer) }
}
See also
SwipeToDismissBoxDefaults: default values and helper functions forSwipeToDismissBox.