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().

The background shown while swiping
The background shown while swiping
// 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

The swipe position. Create it with rememberSwipeToDismissBoxState().

backgroundContent: @Composable RowScope.() -> UnitRequired

What shows behind the item while swiping, usually a color and an icon. Placed in a Row.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

enableDismissFromStartToEnd: BooleanOptional

Default: true

Set to false to block swiping from the start side.

enableDismissFromEndToStart: BooleanOptional

Default: true

Set to false to block swiping from the end side.

gesturesEnabled: BooleanOptional

Default: true

Set to false to turn off swiping.

onDismiss: (SwipeToDismissBoxValue) -> UnitOptionalSince 1.9

Default: {}

Called when the item is swiped away. Receives the direction: StartToEnd or EndToStart.

content: @Composable RowScope.() -> UnitRequired

The item, usually a ListItem.

Longer Example

Swipe a row to the start to delete it, with an "Undo" action in a snackbar.

Swipe a row to the start to delete it, with an undo snackbar
Swipe a row to the start to delete it, with an undo snackbar
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", "[email protected]", favorite = true),
    Contact(2, "Alan Turing", "[email protected]"),
    Contact(3, "Barbara Liskov", "[email protected]"),
    Contact(4, "Grace Hopper", "[email protected]", favorite = true),
    Contact(5, "Katherine Johnson", "[email protected]"),
    Contact(6, "Linus Torvalds", "[email protected]"),
)

/** 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