Author/Developer Relations Engineer Marcel Pintó Biescas, @marxallski
Android 12 improves , which is regarded as a key feature by many Android users- App Widgets . The improved Widgets are more practical, beautiful, and easier to be discovered by users (84% of users use at least one Widgets). Now, we have released the Jetpack Glance , which makes the building process of Widgets easier. This version is built on the new framework of Jetpack Compose runtime (Runtime) Build App Widgets for the home screen or other interfaces faster and easier.
You are welcome to try and do share feedback .
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 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 the combinable item into the actual RemoteViews and display it in App Widgets.
△ Glance structure diagram
This means that Glance needs enable Compose , and it relies on Runtime, Graphics, and the unit interface Compose layer , but it does not have direct interoperability with other existing Jetpack Compose interface elements Sex . However, you can share the state or any other logic in the app to create a clear interface.
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 via GlanceAppWidget and GlanceAppWidgetReceiver
- Glance can use a combination of an initial set of items on your screen: Box, Row, Column , Text , the Button , LazyColumn , Image , Spacer .
- Use the GlanceModifier method to apply modifiers to combinable items.
Use the predefined Action handle user interaction:
- Provide parameters for Action through ActionParameters
- By defining SizeMode.Single , SizeMode.Exact or SizeMode.Responsive treated different layouts Widgets .
- Provide GlanceStateDefinition to retain the GlanceAppWidget state.
- Provide local combinations, such as LocalContext, LocalState, LocalGlanceId, LocalSize .
- By AndroidRemoteViews may be implemented in combination with your existing items RemoteViews interoperation between.
We will also introduce more features, such as default themes, better Android Studio support, and more. Stay tuned new version .
Note: Although the minimum SDK version is 21, the minimum compatible version currently supported by the Alpha version is SDK v23.
You can view the building a new Android App Widgets video in Android 12 for more details.
Glance Getting Started
You can view the example on GitHub to get started quickly. Glance can also be used in the latest stable version of Android Studio , but first you need to follow Jetpack Compose document to set up, because Glance depends on the Compose runtime (Runtime).
In addition, you can also view the advanced example AndroidX code base.
Write your own plugin, you can extend Android Gradle Plugin and customize your build according to your project needs!
△ ResponsiveAppWidget.kt demo
The Alpha version is an important opportunity for you to participate in the development of the API. Therefore, welcome to share your feedback to help us understand and further enhance your experience! You are also welcome to continue to follow us to learn more about the latest content.
Looking forward to your experience!
Welcome to click here to submit feedback to us, or share your favorite content or problems found. Your feedback is very important to us, thank you for your support!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。