Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Compose change tap color

I have a basic row in compose.

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(MaterialTheme.colors.background)
                .clickable { click.invoke() }
        ) {
            Text(
                text = stringResource(id = item.first),
                textAlign = TextAlign.Start,
                color = MaterialTheme.colors.onPrimary,
                modifier = Modifier.padding(16.dp)
            )
        }

When its clicked the color is grey. I would like to change this to a custom color but I can't seem to figure out how.

like image 348
android Avatar asked Mar 27 '26 04:03

android


1 Answers

You can provide it with the indication parameter:

clickable(
    interactionSource = remember { MutableInteractionSource() },
    indication = rememberRipple(color = Color.Red),
) { }

If you want to change it for a group of views, you can provide it with CompositionLocalProvider in LocalIndication:

CompositionLocalProvider(
    LocalIndication provides rememberRipple(color = Color.Red)
) {
    SomeView()
)

Or you can embed it in your theme to apply to the whole application:

@Composable
fun AppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    content: @Composable () -> Unit
) {
    val colors = if (darkTheme) {
        DarkThemeColors
    } else {
        LightThemeColors
    }
    MaterialTheme(
        colors = colors,
        typography = typography,
        shapes = shapes,
    ) {
        CompositionLocalProvider(
            LocalIndication provides rememberRipple(color = Color.Red),
            content = content,
        ) 
    }
}
like image 187
Philip Dukhov Avatar answered Mar 29 '26 13:03

Philip Dukhov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!