Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default button click sound in Jetpack Compose

When implementing the app functionality in a classic Android (XML-based layouts) I had a tap sound enabled by default, I can hear it if an appropriate mode is set on the device.

However, I've noticed that it does not work out of the box for the functionality written in Jetpack Compose. How to enabled the default button click sound without updating every Button and interaction source modifiers?

I tried adding click sound manually, but it creates a mess in a codebase. How to achieve it in a simpler way? Compose is now a preferrable way to develop in Android, so I think it should be some way to do that simpler.

like image 319
yuriid Avatar asked Sep 11 '25 03:09

yuriid


1 Answers

You simply have to add this to your clickable modifier:

val view = LocalView.current

For a custom button:

 modifier = Modifier
            .fillMaxSize()
            .clickable() {
                view.playSoundEffect(SoundEffectConstants.CLICK)}

otherwise:

Button(onClick = { view.playSoundEffect(SoundEffectConstants.CLICK) }) {}

It's not a system-wide solution but it does not require much code.

like image 97
Code Poet Avatar answered Sep 12 '25 18:09

Code Poet