Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Jetpack Compose preview show the wrong string resources?

Sometimes when I use a string resource in my composable, the preview will show the wrong string. It always works fine for literal strings, only string resources are wrong. The bug isn't consistent.

For example if I have this strings.xml:

<resources>
    <string name="app_name">Violit</string>
    <string name="load_topic_failure_message">Something went wrong loading the topic</string>
</resources>

And I have this composable:

@Composable
fun TopicFailureContent() {
    Text(stringResource(R.string.load_topic_failure_message))
}

@Preview(showBackground = true)
@Composable
fun TopicFailureContentPreview() {
    TopicFailureContent()
}

It might render something like "Partially checked" or "Navigation menu" instead of "Something went wrong loading the topic".

If I change the composable to this:

@Composable
fun TopicFailureContent() {
    Text("Something went wrong loading the topic")
}

it renders the preview correctly.

It looks like the preview might be rendering nearby strings instead of the one I want. String resources work fine in tests and running the app. It's just preview that is not always working.

I'm using Android Studio Electric Eel 2022.1.1 but I was having the same problem on the previous version as well. This fails on both Compose UI version 1.2.1 and 1.3.3.

Any idea why string resources don't always work in preview and how to fix it?

like image 503
Eva Avatar asked May 24 '26 13:05

Eva


1 Answers

this problem will appear after editing the resource string file. It solved for me by building the project and then restarting IDE after every edit in resource file.

like image 176
Sadegh Sazmand Avatar answered May 26 '26 05:05

Sadegh Sazmand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!