// Set the colors of a progress bar at runtime.remoteView.setColorStateList(R.id.progress,"setProgressTintList",createProgressColorStateList())// Specify exact sizes for margins.remoteView.setViewLayoutMargin(R.id.text,RemoteViews.MARGIN_END,8f,TypedValue.COMPLEX_UNIT_DP)
Java
// Set the colors of a progress bar at runtime.remoteView.setColorStateList(R.id.progress,"setProgressTintList",createProgressColorStateList());// Specify exact sizes for margins.remoteView.setViewLayoutMargin(R.id.text,RemoteViews.MARGIN_END,8f,TypedValue.COMPLEX_UNIT_DP);
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["# Enhance your widget\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to build widgets using Compose-style APIs. \n[Jetpack Glance →](/develop/ui/compose/glance) \n\n\u003cbr /\u003e\n\nThis page includes details for optional widget enhancements that are available\nstarting in Android 12 (API level 31). These features are optional, but they're\nstraightforward to implement and improve your users' widget experience.\n\nUse dynamic colors\n------------------\n\nStarting in Android 12, a widget can use the device theme colors\nfor buttons, backgrounds, and other components. This provides smoother\ntransitions and consistency across different widgets.\n\nThere are two ways to achieve dynamic colors:\n\n- Use the system's default theme\n (`@android:style/Theme.DeviceDefault.DayNight`) in the root layout.\n\n- Use the Material 3 theme (`Theme.Material3.DynamicColors.DayNight`) from the\n [Material Components for\n Android](https://github.com/material-components/material-components-android)\n library, available starting in [Material Components for Android\n v1.6.0](https://github.com/material-components/material-components-android/releases/tag/1.6.0).\n\nOnce the theme is set in the root layout, you can use common color attributes in\nthe root or any of its children to pick up the dynamic colors.\n\nSome examples of color attributes you can use are the following:\n\n- `?attr/primary`\n- `?attr/primaryContainer`\n- `?attr/onPrimary`\n- `?attr/onPrimaryContainer`\n\nIn the following example using the Material 3 theme, the device's theme color is\n\"purplish.\" The accent color and widget background adapt for light and dark\nmodes, as shown in figures 1 and 2. \n\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:background=\"?attr/colorPrimaryContainer\"\n android:theme=\"@style/Theme.Material3.DynamicColors.DayNight\"\u003e\n\n \u003cImageView\n ...\n app:tint=\"?attr/colorPrimaryContainer\"\n android:src=\"@drawable/ic_partly_cloudy\" /\u003e\n\n \u003c!-- Other widget content. --\u003e\n\n \u003c/LinearLayout\u003e\n\n**Figure 1.** Widget in light theme. **Figure 2.** Widget in dark theme.\n\n\u003cbr /\u003e\n\n| **Tip:** We recommend using the [Material 3](https://m3.material.io) theme to follow Material Design guidelines for consistency between devices and for backward compatibility.\n\n### Backward compatibility for dynamic colors\n\nDynamic colors are only available in devices running Android 12\nor higher. To provide a custom theme for lower versions, create a default theme\nwith your custom colors and a new qualifier (`values-v31`) using the default\ntheme attributes.\n\nHere is an example using the Material 3 theme: \n\n### `/values/styles.xml`\n\n \u003cresources\u003e\n \u003cstyle name=\"MyWidgetTheme\" parent=\"Theme.Material3.DynamicColors.DayNight\"\u003e\n \u003c!-- Override default colorBackground attribute with custom color. --\u003e\n \u003citem name=\"android:colorBackground\"\u003e@color/my_background_color\u003c/item\u003e\n\n \u003c!-- Add other colors/attributes. --\u003e\n\n \u003c/style\u003e\n \u003c/resources\u003e\n\n### `/values-v31/styles.xml`\n\n \u003cresources\u003e\n \u003c!-- Do not override any color attribute. --\u003e\n \u003cstyle name=\"MyWidgetTheme\" parent=\"Theme.Material3.DynamicColors.DayNight\" /\u003e\n \u003c/resources\u003e\n\n### `/layout/my_widget_layout.xml`\n\n \u003cresources\u003e\n \u003cLinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n ...\n android:background=\"?android:attr/colorBackground\"\n android:theme=\"@style/MyWidgetTheme\" /\u003e\n \u003c/resources\u003e\n\n| **Note:** See [Styles and Themes](/guide/topics/ui/look-and-feel/themes) for an in-depth guide to handling different styles, colors, attributes, and themes.\n\nEnable voice support\n--------------------\n\n[App Actions](//developers.google.com/assistant/app) let Google Assistant\ndisplay widgets in response to relevant user voice commands. By configuring your\nwidget to respond to [built-in intents (BIIs)](/guide/app-actions/intents), your\napp can proactively display widgets on Assistant surfaces such as Android and\nAndroid Auto. Users have the option to\n[pin widgets](/guide/app-actions/widgets#pinning) displayed by Assistant to\ntheir launcher, encouraging future engagement.\n\nFor example, you can configure the workout summary widget for your exercise app\nto fulfill the user voice commands that trigger the\n[`GET_EXERCISE_OBSERVATION`](/reference/app-actions/built-in-intents/health-and-fitness/get-exercise-observation)\nBII. Assistant proactively displays your widget when users trigger this BII by\nmaking requests like, *\"Hey Google, how many miles did I run this week on\nExampleApp?\"*\n\nThere are dozens of BIIs covering several categories of user interaction,\nletting almost any Android app enhance their widgets for voice. To get started,\nsee [Integrate App Actions with Android widgets](/guide/app-actions/widgets).\n\nAdd a name to your widget\n-------------------------\n\nWidgets need to have a unique name when they are displayed in the widget picker.\n\nWidgets' names are loaded from the `label` attribute of the widget's `receiver`\nelement in the AndroidManifest.xml file. \n\n \u003creceiver\n ....\n android:label=\"Memories\"\u003e\n ....\n \u003c/receiver\u003e\n\nAdd a description for your widget\n---------------------------------\n\nStarting in Android 12, provide a description for the widget\npicker to display for your widget.\n**Figure 3.** Sample widget picker showing a widget and its description.\n\nProvide a description for your widget using the `description` attribute of the\n`<appwidget-provider>` element: \n\n \u003cappwidget-provider\n android:description=\"@string/my_widget_description\"\u003e\n \u003c/appwidget-provider\u003e\n\n| **Note:** Be concise. There is no character limit, but the representation and available space for the description might differ depending on the device.\n\nYou can use the\n[`descriptionRes`](/reference/android/appwidget/AppWidgetProviderInfo#descriptionRes)\nattribute on previous versions of Android, but it is ignored by the widget\npicker.\n\nEnable smoother transitions\n---------------------------\n\nStarting in Android 12, launchers provide a smoother transition\nwhen a user launches your app from a widget.\n\nTo enable this improved transition, use `@android:id/background` or\n`android.R.id.background` to identify your background element: \n\n // Top-level layout of the widget.\n \u003cLinearLayout\n android:id=\"@android:id/background\"\u003e\n \u003c/LinearLayout\u003e\n\n| **Warning:** Avoid using [broadcast\n| trampolines](/about/versions/12/behavior-changes-12#notification-trampolines). Starting in Android 12, an app can still launch an activity from a broadcast receiver or service if it's initiated from a widget click's [`PendingIntent`](/reference/android/app/PendingIntent). However, the new app animation isn't used for apps launched from a broadcast receiver or service, which leads to a poor user experience.\n\nYour app can use `@android:id/background` on previous versions of Android\nwithout breaking, but it is ignored.\n\nUse runtime modification of RemoteViews\n---------------------------------------\n\nStarting in Android 12, you can take advantage of several\n`RemoteViews` methods that provide for runtime modification of `RemoteViews`\nattributes. See the [`RemoteViews`](/reference/android/widget/RemoteViews) API\nreference for the full list of added methods.\n\nThe following code example shows how to use a few of these methods. \n\n### Kotlin\n\n```kotlin\n// Set the colors of a progress bar at runtime.\nremoteView.setColorStateList(R.id.progress, \"setProgressTintList\", createProgressColorStateList())\n\n// Specify exact sizes for margins.\nremoteView.setViewLayoutMargin(R.id.text, RemoteViews.MARGIN_END, 8f, TypedValue.COMPLEX_UNIT_DP)\n```\n\n### Java\n\n```java\n// Set the colors of a progress bar at runtime.\nremoteView.setColorStateList(R.id.progress, \"setProgressTintList\", createProgressColorStateList());\n\n// Specify exact sizes for margins.\nremoteView.setViewLayoutMargin(R.id.text, RemoteViews.MARGIN_END, 8f, TypedValue.COMPLEX_UNIT_DP);\n```"]]