'Jetpack Compose custom layout clicks don't work
I created my own class called MyInputRow.
@Composable
fun MyInputRow(
title: String,
modifier: Modifier = Modifier,
bgColor: Color,
icon: Painter,
clickAction: Unit
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(6.dp)
.background(color = bgColor)
.clickable { clickAction },
verticalAlignment = Alignment.CenterVertically,
)
{
Text(
modifier = Modifier
.weight(1f)
.padding(6.dp)
.wrapContentWidth(Alignment.Start),
text = title,
)
Image(
painter = icon,
contentDescription = "icon"
)
}
}
But, this clickAction does not work.
Specifically, clickAction is called for some reason when the screen is displayed, and clickAction is not called when Row is clicked.
It is called as follows.
class AddActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
setContent {
MyTheme {
...
AddMain()
}
}
}
}
@Composable
fun AddMain() {
BarcodeLayout()
},
}
@Composable
fun BarcodeLayout() {
Column(Modifier.fillMaxWidth()) {
Text(text = {...})
MyInputRow(
title = ...,
bgColor = ...,
icon = ...,
clickAction = scanBarcode()
)
}
}
fun scanBarcode() {
Timber.d("call scanBarcode")
}
I don't know why. Please help me.
Solution 1:[1]
There are 2 options you can try.
Option 1:
.clickable { clickAction() }
or Option 2:
.clickable(clickAction)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | krupa parekh |
