An <appwidget-provider>
expects a previewLayout
attribute on modern Android versions. The documentation tells us what value we should use for initialLayout
, but it never mentions the previewLayout
attribute. Am I supposed to try to completely rebuild my Glance UI in XML?
No, you do not need to rebuild your entire Glance
UI in XML
for the previewLayout
attribute of <appwidget-provider>
. The previewLayout
is used only by the launcher or home screen
to display a static preview of your widget in the picker before adding it to the home screen. It does not impact your runtime Glance-based UI.
Make a simple XML layout similar to the final form of the widget, for almost preview purposes.
It doesn't have to be interactive or dynamic - this is just a stable image shown in the widget picker.
This file goes to the res/ layout/ and must be referred to preview.
For Example
res/layout/widget_preview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="100dp"
android:orientation="vertical"
android:background="@android:color/darker_gray"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Widget Preview"
android:textSize="18sp"
android:textColor="@android:color/white" />
</LinearLayout>
res/xml/my_widget_info.xml
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="100dp"
android:updatePeriodMillis="0"
android:previewLayout="@layout/widget_preview"
android:initialLayout="@layout/placeholder"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
Note: you can directly use screenshot as well in preview using the attributes
android:previewImage="@drawable/widget_big"
or for XML
android:previewLayout="@layout/widget_preview"
And finally in manifest file*
<receiver android:name=".SampleReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/my_widget_info" />
</receiver>