Like title said I need to get text from string.xml and the problem is that it gets this error:
@Composable invocations can only happen from the context of a @Composable function
@Composable
fun buttonClick() {
var text = ""
//needs this modifier for component click
var modifier: Modifier = Modifier.clickable() {
text = stringResource(id = R.string.app_name) //this is were warning is
}
}
This is what I need inside of function, how to get it
.clickable {
stringResource(id = R.string.app_name)
}
>Solution :
Try this,
val context = LocalContext.current
var text = ""
Button(
onClick = {
text = context.getString(R.string.app_name)
}
) {
Text(text = "Hit me")
}
You can use LocalContext to access getString.