Unable to use TextClock in GlanceAppWidget as remote View

152 views Asked by At

Based on the official documentation,

enter image description here

I can see TextClock seems to be supported now in new GlanceAPI as a Remote view, but I can not used in our example code,

 class RemoteWidget : GlanceAppWidget(errorUiLayout = R.layout.widget_error_layout) {

override val stateDefinition: GlanceStateDefinition<*>
    get() = CustomGlanceStateDefinition

@Composable
override fun Content() {

    val pref = currentState<Preferences>()
    Column(
        modifier = GlanceModifier
            .fillMaxSize()
            .background(color = Color.Green)
            .padding(8.dp)
    ) {
        Text(
            text = LocalContext.current.getString(R.string.app_name)
        )
        TextClock() //Error
        AndroidView(factory = {     TextClock(it)  }) //Error
        
    }
}

I don't know how to use TextClock in GlanceWidget

1

There are 1 answers

0
Marcel On

Glance has indeed interoperability with all available RemoteViews. You need to use the AndroidRemoteView composable part of Glance.

Note that you are using the AndroidView composable instead.

Here is an example:

val packageName = LocalContext.current.packageName
Column(modifier = GlanceModifier.fillMaxSize()) {
   Text("My old button")
   AndroidRemoteViews(RemoteViews(packageName, R.layout.my_awesome_old_button))
}