Home > Software design >  How do you change card colors on material3 android when pressed?
How do you change card colors on material3 android when pressed?

Time:02-02

enter image description here


Otherwise you can use the clickable modifier:

val interactionSource = remember { MutableInteractionSource() }
Card(
    modifier = Modifier
        .size(width = 180.dp, height = 100.dp)
        .clickable (
            onClick = { /* Do something */ },
            interactionSource = interactionSource,
            indication = rememberRipple(color = Green )
        )
) {
    //Card content
}

enter image description here


Finally if you want to modify the background color when the Card is pressed (not the ripple effect) you can use:

    val interactionSource = remember { MutableInteractionSource() }
    val isPressed by interactionSource.collectIsPressedAsState()
    val backgroundColor = if (isPressed) Yellow else MaterialTheme.colorScheme.surfaceVariant

    Card(
        interactionSource = interactionSource,
        onClick = { /* Do something */ },
        modifier = Modifier
            .size(width = 180.dp, height = 100.dp),
        colors = CardDefaults.cardColors(
            containerColor = backgroundColor
        )

    ) {
        //Card content
    }

enter image description here

  • Related