I have button to record voice so I want it to start record when user press it and stop when he leave it
@Composable
fun Screen(){
Button(){
Text("record")
}
}
I have button to record voice so I want it to start record when user press it and stop when he leave it
@Composable
fun Screen(){
Button(){
Text("record")
}
}
Gabriele Mariotti
On
To get the press/release actions in a Button you can use the InteractionSource.collectIsPressedAsState to know if the Button is pressed.
You can add a side effect to know when the Button is released.
Something like:
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
if (isPressed){
println("Pressed")
//Use if + DisposableEffect to wait for the press action is completed
DisposableEffect(Unit) {
onDispose {
println("released")
}
}
}
Button(onClick={},
interactionSource = interactionSource
){
Text("record")
}

If you're asking just about the press/release actions, I don't know how to do this with a button, but you can achieve the same result using a
Box(for instance) and use some modifiers to design it the way you want...Here is a possible solution.
Notice that I'm using a
Boxwith a similar design of aButton.Here is the result: