Search code examples
androidglance

Why doesn't the clickable modifier work for a Glance Widget?


I have a very simple widget that I'm making with Glance, something like this:

 class MyWidget : GlanceAppWidget() {

    override suspend fun provideGlance(context: Context, id: GlanceId) {
        provideContent {
            WidgetContent(GlanceModifier.fillMaxSize(), context)
        }
    }

    @Composable
    private fun WidgetContent(modifier: GlanceModifier = GlanceModifier, context: Context) {
        Column(
            modifier = modifier.background(R.color.white).clickable {
                actionStartActivity<MainActivity>()
            },
            verticalAlignment = Alignment.CenterVertically,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = context.getString(R.string.foo),
                modifier = GlanceModifier.padding(12.dp)
            )
            Button(
                text = "Foo",
                onClick = actionStartActivity<MainActivity>()
            )
        }
    }

in this example, tapping on Button will launch my activity, however, the clickable modifier doesn't. I've seen this question and this is potentially a duplicate, unless there's a reason for that behaviour ? Perhaps it's impossible to do this for a specific reason ? I don't want to add a button here, I want the entire widget to respond to a click/tap, how can I do that ?

Glance version:

implementation("androidx.glance:glance-appwidget:1.0.0")
implementation("androidx.glance:glance-material3:1.0.0")

Adding the .clickable block to only the Text doesn't solve the problem either, perhaps .clickable just doesn't work with Glance yet?


Solution

  • Found the solution:

    actionStartActivity is an action type, as such it can only be passed to modifiers, it can't be invoked as a lambda function.

    instead of:

    .clickable {
       actionStartActivity<MainActivity>()
    },
    

    it should be:

    .clickable(actionStartActivity<MainActivity>())