0
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?

RemoteViewsをActivity上に表示する

Posted at

AppWidgetやカスタムNotificationでRemoteViewsを扱うことがあると思いますが、このRemoteViewsはAppWidgetやNotificationとしてしか使えない、訳ではありません。
直接アプリ上に表示することもできます。

テンプレートでAppWidgetを作ったものをシンプルに整理すると以下のようになります

class MyAppWidget : AppWidgetProvider() {
    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        for (appWidgetId in appWidgetIds) {
            val views = RemoteViews(context.packageName, R.layout.my_app_widget)
            appWidgetManager.updateAppWidget(appWidgetId, views)
        }
    }
}

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

RemoteViewsはこれですね

val views = RemoteViews(context.packageName, R.layout.my_app_widget)

これをActivity上に表示してみましょう。
とりあえずコンテナとしてFrameLayoutを配置し、そこに表示差せます。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

RemoteViewsとはどういうものか?というのを端的に言えば、他のアプリにViewを表示してもらうための、ParcelableなViewの構築情報と言えます。ホームアプリやシステムUIはRemoteViewsを受け取り、Viewを構築して表示しているわけです。つまり、RemoteViewsの情報を使ってViewを作ることができます。

それはRemoteViewsが持つ、applyメソッドです。
以下のように使います。

val views = RemoteViews(packageName, R.layout.my_app_widget)
val view = views.apply(applicationContext, binding.container)
binding.container.addView(view)

これで以下のようにActivity上にRemoteViewsを表示することができます。

applyメソッドの第一引数のcontextは通常applicationContextを指定します。
これによって、RemoteViewsのレイアウトからViewがInflateされ、RemoteViewsに渡した各種設定を反映したViewが返却されます。


ウィジェットのプレビュー機能が必要な場合に、同様のレイアウトのViewを別に作ったりする必要は無く、RemoteViews作成ロジックをそのまま使ってプレビュー表示をさせる、といった用途で活躍しそうです。
ホームアプリのウィジェットのようにプロセス間通信で他のアプリからRemoteViewsを受け取って表示する機能を作ることもできますね。

以上です。

0
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
0
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?