Search code examples
androidandroid-jetpack-composeandroid-appwidgetglance

E/GlanceAppWidget: Error in Glance App Widget IllegalStateException: CompositionLocal LocalConfiguration not present


I am developing an app widget with compose. I have a problem with getting string from resources. If I give text as a string like text = "Label", it is working properly. But if I give with stringResource, text is not shown, and I see this problem. Is there anyone who has faced this issue? Thank you

E/GlanceAppWidget: Error in Glance App Widget
    java.lang.IllegalStateException: CompositionLocal LocalConfiguration not present
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.noLocalProvidedFor(AndroidCompositionLocals.android.kt:167)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt.access$noLocalProvidedFor(AndroidCompositionLocals.android.kt:1)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$LocalConfiguration$1.invoke(AndroidCompositionLocals.android.kt:47)
        at androidx.compose.ui.platform.AndroidCompositionLocals_androidKt$LocalConfiguration$1.invoke(AndroidCompositionLocals.android.kt:44)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at androidx.compose.runtime.LazyValueHolder.getCurrent(ValueHolders.kt:29)
        at androidx.compose.runtime.LazyValueHolder.getValue(ValueHolders.kt:31)
        at androidx.compose.runtime.ComposerImpl.resolveCompositionLocal(Composer.kt:1776)
        at androidx.compose.runtime.ComposerImpl.consume(Composer.kt:1746)
        at androidx.compose.ui.res.StringResources_androidKt.resources(StringResources.android.kt:78)
        at androidx.compose.ui.res.StringResources_androidKt.stringResource(StringResources.android.kt:36)
class MarketWidget : GlanceAppWidget() {

    @Composable
    override fun Content() {
        Column(
            modifier = GlanceModifier
                .fillMaxSize()
                .background(color = Color.White)
                .padding(8.dp)
        ) {
            Text(
                text = stringResource(id = R.string.app_name)
            )
        }
    }
}

Solution

  • stringResource is part of Compose UI, so you can't use it in Glance. Glance's Text only takes resolved strings, you can retrieve it using LocalContext.current.

    To change your example, this should work:

    Text(text = LocalContext.current.getString(R.string.app_name))
    

    Also, ensure that you're importing the Glance Text (androidx.glance.text.Text) and not the one from Material Compose UI (androidx.compose.material.Text)