For a android.widget.Button I can use performClick() to simulate a click programmatically. But I don't know how to do it in Jetpack Compose. I've looked into the compose.material documentation but I at least coudn't find anything about this.
For a android.widget.Button I can use performClick() to simulate a click programmatically. But I don't know how to do it in Jetpack Compose. I've looked into the compose.material documentation but I at least coudn't find anything about this.
In compose you're creating a button with action. You can pass action function and call same function programmatically.
// you need to remember your callback to prevent extra recompositions.
// pass all variables that may change between recompositions
// as keys instead of `Unit`
val onClickAction = remember(Unit) {
{
// do some action
}
}
LaunchedEffect(Unit) {
// perform action in 1 sec after view appear
delay(1000)
onClickAction()
}
Button(
onClick = onClickAction
) {
Text(text = "Button")
}
I'd write an instrumentation test with ComposeTestRule for this. performClick() generally works as expected for me on a SemanticNodeInteraction object.
I did run into a problem where it only worked when the element was currently visible though, so I needed to call performScrollTo() first:
composeTestRule.onNode(...).run {
performScrollTo()
performClick()
}
Here you go
Button(
onClick = {
// here you can perform you action on button click
},
modifier = Modifier
.fillMaxWidth()
.height(55.dp)
.background(
color = WoodSmoke,
shape = RoundedCornerShape(30)
),
colors = ButtonDefaults.buttonColors(backgroundColor =
Color.Transparent),
) {
Text(text = "Sign in", color = Color.White)
}