I have a button and I want it to execute an action only while it is long pressed.
I am able to execute the action on the long press already. The problem is: I do not know how to stop it once the user is no longer pressing down on the button.
How do I go about implementing something like this?
I am using Jetpack Compose on an Android App.
To do this, the
Button
has an argumentinteractionSource
. It can be used as follows:If you need to perform some action until the button is released, you can use
isPressed
withLaunchedEffect
:It is launched in a coroutine scope, which will be canceled as soon as
isPressed
becomesfalse
.An other option is using it with
DisposableEffect
: