1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Jetpack GlanceをActivity上に表示する

Posted at

RemoteViewsはViewに変換し、Activity上に表示することができます。

Jetpack Glanceの実体はComposeの構文を使ってRemoteViewsを作成する仕組みなので、同様にActivity上に表示することができます。

適当にGlanceAppWidgetを実装してみます。

class MyGlanceWidget : GlanceAppWidget() {
    override suspend fun provideGlance(context: Context, id: GlanceId) {
        provideContent {
            Box(
                modifier = GlanceModifier
                    .fillMaxSize()
                    .padding(24.dp)
                    .background(Color(0xFF0000FF))
                    .cornerRadius(16.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    modifier = GlanceModifier
                        .padding(16.dp)
                        .background(Color(0xFFFF0000))
                        .cornerRadius(16.dp),
                    text = "Hello World"
                )
            }
        }
    }
}

ウィジェットとして表示すると以下のようになります。

GlanceAppWidgetのロジックを通してRemoteViewsを作成するには、拡張関数 composeを使います。

RemoteViewsを作ることができたら、あとはRemoteViewsを表示する場合と同じです。

lifecycleScope.launch {
    val views = MyGlanceWidget().compose(this@MainActivity)
    val view = views.apply(applicationContext, binding.container)
    binding.container.addView(view)
}

Activity上にウィジェットと同じレイアウトが表示されました。

以上です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?