Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Needs to get value from string.xml on button onclick. @Composable invocations can only happen from the context of a @Composable function

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

.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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading