Mokera

Tech Sheets

mokelab

Display a text in Jetpack Compose

Last updated:2024-07-10

To display a text with Jetpack Compose, use the Text composable function. Specify the string you want to display as an argument.

@Composable
fun Greeting(name: String) {
    Text(text = "Hello $name!")
}

use string resources

To support multiple languages, it is good to use string resoucrs.

We use stringResource() in Jetpack Compose. Argument is a string resource ID like getString() .

@Composable
fun AppName() {
    Text(text = stringResource(id = R.string.app_name))
}

If string resource has a placeholder like %1$s , you can pass a placeholder value like getString() .

@Composable
fun Greeting(name: String) {
    Text(text = stringResource(id = R.string.hello, name))
}

Back