Advanced Android 12 Widget Features: Reconfigurable Widgets, Default Configurations, Size Limits, and New APIs

This article explains how Android 12 introduces reconfigurable widgets, default configuration flags, new size attributes, responsive layouts, compound button interactions, and simplified collection RemoteViews, providing developers with richer, more interactive widget experiences on mobile devices.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Advanced Android 12 Widget Features: Reconfigurable Widgets, Default Configurations, Size Limits, and New APIs

This article is the second part of the "Updating Android 12 Widgets" series and explores advanced widget features that make Android 12 widgets more interactive, easier to configure, and capable of delivering a better UI experience.

Simpler configuration : Android 12 allows users to reconfigure an existing widget without deleting it. Developers enable this by setting the android:widgetFeatures="reconfigurable" attribute in the appwidget-provider XML.

<appwidget-provider
   android:configure="com.example.android.appwidget.ListWidgetConfigureActivity"
   android:widgetFeatures="reconfigurable"
   ... />

Default configuration : By adding the configuration_optional flag together with reconfigurable, a widget can provide a default layout that skips the initial configuration step, while still allowing later re‑configuration.

<appwidget-provider
   android:configure="com.example.android.appwidget.ListWidgetConfigureActivity"
   android:widgetFeatures="reconfigurable|configuration_optional"
   ... />

New size attributes : Android 12 adds maxResizeWidth, maxResizeHeight, targetCellWidth, and targetCellHeight. Devices running Android 12 use the target cell attributes for default widget size, while older versions fall back to minWidth and minHeight.

<appwidget-provider
   android:maxResizeWidth="240dp"
   android:maxResizeHeight="180dp"
   android:minWidth="180dp"
   android:minHeight="110dp"
   android:minResizeWidth="180dp"
   android:minResizeHeight="110dp"
   android:targetCellWidth="3"
   android:targetCellHeight="2"
   ... />

Responsive layouts : Developers can supply multiple layout resources and let the system switch them automatically based on widget size using updateAppWidget() with a mapping of SizeF to RemoteViews.

val viewMapping = mapOf(
   SizeF(150f, 110f) to RemoteViews(context.packageName, R.layout.widget_grocery_list),
   SizeF(250f, 110f) to RemoteViews(context.packageName, R.layout.widget_grocery_grid)
)
appWidgetManager.updateAppWidget(appWidgetId, RemoteViews(viewMapping))

Compound button interaction : Widgets can now include interactive buttons using RemoteResponse.fromPendingIntent(). For collection items, a PendingIntent template with RemoteResponse.fromFillInIntent() is recommended for better performance.

remoteViews.setOnCheckedChangeResponse(
   R.id.item_switch,
   RemoteViews.RemoteResponse.fromPendingIntent(onCheckedChangePendingIntent)
)

remoteViews.setOnCheckedChangeResponse(
   R.id.item_switch,
   RemoteViews.RemoteResponse.fromFillInIntent(onCheckedChangeFillInIntent)
)

Simplified collection RemoteViews : Android 12 introduces setRemoteAdapter() with RemoteCollectionItems.Builder, eliminating the need for a RemoteViewsService and RemoteViewsFactory when populating collections.

remoteViews.setRemoteAdapter(
   R.id.items_list_view,
   RemoteViews.RemoteCollectionItems.Builder()
       .addItem(ID_1, RemoteViews(...))
       .addItem(ID_2, RemoteViews(...))
       .setViewTypeCount(MAX_NUM_DIFFERENT_REMOTE_VIEWS_LAYOUTS)
       .build()
)

In conclusion, updating your widgets to Android 12 provides a fresh look and richer interactivity; refer to the linked GitHub samples for complete code examples and stay tuned for future widget enhancements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mobile developmentAndroidKotlinXMLAppWidgetWidget Configuration
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.