According to Google Developers, Android 12 has improved a feature that many Android users regard as key-App Widgets. The improved Widgets are more practical, beautiful, and easier to be discovered by users ( 84% of users use at least one Widget).
Now, we have released the first Alpha version of Jetpack Glance to make the building process of Widgets easier. This version is a new framework built on the Jetpack Compose runtime (Runtime), designed to achieve faster and easier Build App Widgets for the main screen or other interfaces.
Android 12 Widgets Improvements
App Widgets
Jetpack Glance
Jetpack Compose runtime (Runtime)
You are welcome to start experimenting and share feedback:
https://issuetracker.google.com/issues/new?component=1097239& ;template=1611667
Glance provides a new declarative Kotlin API, which is similar to the API you use in Jetpack Compose, which can help you build more beautiful and adaptive App Widgets with less code.
Glance Hello World Widgets example
class GreetingsWidget ( private val name : String ): GlanceAppWidget () { @Composable override fun Content () { Text ( text = "Hello $name" ) } } class GreetingsWidgetReceiver : GlanceAppWidgetReceiver () { override val glanceAppWidget = GreetingsWidget ( "Glance" ) }
Working Principle
Glance provides a set of basic components that can be combined to help you create a “clear” experience. Now, we will provide more App Widgets. Glance uses Jetpack Compose runtime (Runtime) to convert compostable items into actual RemoteViews and display them in App Widgets.
Glance Structure Diagram
Combinable items
RemoteViews
This means that Glance needs to enable Compose and relies on the runtime, graphics and unit interface Compose layers, but it does not have direct interoperability with other existing Jetpack Compose interface elements. However, you can share the state or any other logic in the app to create a clear interface.
Enable Compose
Compose layer
Alpha-version introduction
This initial version introduces the main API, which not only enables interoperability with existing RemoteViews but also helps you build App Widgets.
The following is an overview of the content provided by the development library:
Declare your App Widgets through GlanceAppWidget and GlanceAppWidgetReceiver.
GlanceAppWidget
GlanceAppWidgetReceiver
Combine your interface with the initial set of combinable items in Glance: Box, Row, Column, Text, Button, LazyColumn, Image, Spacer.
Box, Row, Column, Spacer
Text
Button
LazyColumn
Image
Use the GlanceModifier method to apply modifiers to combinable items:
Use predefined Actions to handle user interactions:
actionStartActivity
actionRunCallback
actionStartService
actionStartBroadcastReceiver
Provide parameters for Action through ActionParameters:
Handle different Widgets layouts by defining SizeMode.Single, SizeMode.Exact or SizeMode.Responsive.
SizeMode.Single
SizeMode.Exact
SizeMode.Responsive
Different Widgets layout
Provide GlanceStateDefinition to preserve GlanceAppWidget state.
GlanceStateDefinition
GlanceAppWidget
- https://developer.android.google.cn/reference/kotlin/androidx/glance/appwidget/state/package-summary
- Provide local combinations, such as LocalContext, LocalState, LocalGlanceId, LocalSize:
- https://developer.android.google.com/reference/kotlin/androidx/glance/package-summary#top-level-properties
- Interoperate with your existing RemoteViews through the composable items of AndroidRemoteViews.
AndroidRemoteViews
RemoteViews
- https://developer.android.google.cn/reference/android/widget/RemoteViews.html
- We will also introduce more features, such as default themes, better Android Studio support, and more. Stay tuned for the new version:
- https://developer.android.google.cn/jetpack/androidx/releases/glance
- Note: Although the minimum SDK version is 21, the minimum compatible version currently supported by the Alpha version is SDK v23.
△ Build new Android App Widgets in Android 12
Getting started with Glance
You can view the examples on GitHub to get started quickly. You can also use Glance in the latest stable version of Android Studio, but first you need to follow the steps in the Jetpack Compose document to set it up, because Glance relies on the Compose runtime (Runtime).
Example
- https://github.com/android/user-interface-samples/tree/glance/AppWidget/glance-widget
- The stable version of Android Studio
- https://developer.android.google.cn/studio
- Jetpack Compose documentation
- https://developer.android.google.cn/jetpack/compose/interop/adding#setup
- In addition, you can also view advanced examples in the AndroidX codebase:
- https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:glance/glance-appwidget/integration-tests/demos/src/main/java/androidx/glance/appwidget/demos/
Write your own plugin, you can extend Android Gradle Plugin and customize your build according to your project needs!
ResponsiveAppWidget.kt demo
ResponsiveAppWidget.kt