I use the Wear Compose Version 1.2.1, which can handle onLongClick event with Modifier.combinedClickable(), but after the Wear Compose Version 1.3.x does not handle the long click event.
I think the feature has changed, but I don't know how to modify my code. Can anyone please suggest how can I convert the below-mentioned code for the Wear Compose Version 1.2.1,
Button(
modifier = Modifier
.height(48.dp)
.width(48.dp)
.padding(2.dp)
.background(color = Color.Black)
.combinedClickable(
interactionSource = remember {
MutableInteractionSource()
},
indication = null,
enabled = true,
onClick = {
Toast.makeText(
context,
context.getString(R.string.long_press_to_stop),
Toast.LENGTH_SHORT
).show()
},
onLongClick = {
Log.v("STOP", "onLongClick")
// --- stop action ---
},
),
onClick = { },
colors = ButtonDefaults.primaryButtonColors(backgroundColor = Color.Black),
enabled = true
) {
Icon(
painter = painterResource(id = R.drawable.baseline_stop_24),
contentDescription = "Stop",
tint = Color.LightGray
)
}
The dependency section of my build.gradle.kts, just change the version number, then change the behaviour.
//val wearComposeVersion = "1.2.1" // works OK
val wearComposeVersion = "1.3.0" // does not handle long click event
implementation("androidx.wear.compose:compose-material:$wearComposeVersion")
implementation("androidx.wear.compose:compose-foundation:$wearComposeVersion")
implementation("androidx.wear.compose:compose-navigation:$wearComposeVersion")
Solution
In a nutshell:
Boxas the root content of theButton.combinedClickablemodifier to theBox, with theonLongClick.clearAndSetSemanticsfromButtonin order to addonLongClickfor accessibility.You can see how it was done in Button.
Alternative solution
Use Button component from Horologist Compose Material library.
Dependency:
Import:
Usage:
Context
Yes, as per findings of a member of the Wear Compose team:
Be mindful that the solution above is tailored for
Buttoncomponent. There are more steps for other components with inner paddings likeChipandCardas per this comment.