setWidgetPreviews

Function

Android
@RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
suspend inline fun <reified T : GlanceAppWidgetReceiver> GlanceAppWidgetManager.setWidgetPreviews(
    widgetCategories: IntSet =
        intSetOf(
            WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX
        ),
): Boolean

Generate and publish the widget previews for a GlanceAppWidgetReceiver for the given set of widgetCategories. Previews are generated from the layout provided by GlanceAppWidget.providePreview on the widget connected to the given GlanceAppWidgetReceiver class. This receiver must be registered as an app widget provider in the application manifest.

Previews should be published during the initial setup phase or launch of your app. To avoid running this unnecessarily, you can see what previews are currently published for your provider by checking AppWidgetProviderInfo.generatedPreviewCategories.

The preview composition is run for each value in the widgetCategories array. If your widget has the same layout across categories, you can combine all of the categories in a single value, e.g. WIDGET_CATEGORY_HOME_SCREEN or WIDGET_CATEGORY_KEYGUARD or WIDGET_CATEGORY_SEARCHBOX, which will call composeForPreview once and set the previews for all of the categories.

Note that this API is only available on Build.VERSION_CODES.VANILLA_ICE_CREAM and above, so you will likely want to set AppWidgetProviderInfo.previewLayout and AppWidgetProviderInfo.previewImage as well to have the most coverage across versions.

See also AppWidgetProviderInfo.generatedPreviewCategories, AppWidgetManager.setWidgetPreview, AppWidgetManager.getWidgetPreview, and AppWidgetManager.removeWidgetPreview.

Parameters

widgetCategoriesthe widget categories for which to set previews. Each element of this set must be a combination of WIDGET_CATEGORY_HOME_SCREEN, WIDGET_CATEGORY_KEYGUARD, or WIDGET_CATEGORY_SEARCHBOX.

Returns

true if the preview was successfully updated, false if otherwise. AppWidgetManager.setWidgetPreview will return false when the caller has hit a system-defined rate limit on setting previews for a particular provider. In this case, you may opt to schedule a task in the future to try again, if necessary.