onTouchDown / Up / Cancel
Pressed while held, with a count of touches.
Open the live, interactive demo →
Change its controls, read the remote document it produces and the diff after every change.
What it is
Reacts to the moment a finger goes down, lifts, or slides away, for press-and-hold feedback.
Good for
- Pressed states
- Hold-to-confirm buttons
Code
The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.
import androidx.compose.remote.creation.compose.action.combinedAction
import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.onTouchCancel
import androidx.compose.remote.creation.compose.modifier.onTouchDown
import androidx.compose.remote.creation.compose.modifier.onTouchUp
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.MutableRemoteInt
import androidx.compose.remote.creation.compose.state.rb
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.ri
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
@Composable
@RemoteComposable
fun TouchEvents(pressedColor: Color) {
val pressed = remember { MutableRemoteBoolean(false) }
val downs = remember { MutableRemoteInt(0) }
RemoteBox(
RemoteModifier.size(220.rdp, 120.rdp)
.clip(RemoteRoundedCornerShape(16.rdp))
.background(pressed.select(pressedColor.rc, Color(0xFFB0BEC5).rc))
.onTouchDown(
combinedAction(
valueChangeAction(pressed, true.rb),
valueChangeAction(downs, downs + 1.ri),
),
)
.onTouchUp(valueChangeAction(pressed, false.rb))
.onTouchCancel(valueChangeAction(pressed, false.rb)),
contentAlignment = RemoteAlignment.Center,
) {
RemoteText(("Press and hold · downs: ".rs + downs.toRemoteString()), color = Color.White.rc)
}
}
API
creation.compose.modifier.onTouchDown
RemoteModifier onTouchDown(RemoteModifier, RemoteAction action)creation.compose.modifier.onTouchUp
RemoteModifier onTouchUp(RemoteModifier, RemoteAction action)creation.compose.modifier.onTouchCancel
RemoteModifier onTouchCancel(RemoteModifier, RemoteAction action)