Author/developer relations Engineer Marcel Pinto Biescas, @marxallski

Android 12 has improved App Widgets, a feature that many Android users see as key, to make them more useful, beautiful, and easier to discover (84% of users use at least one widget). Now, we’re making it easier to build Widgets with the first Alpha release of Jetpack Glance, a new framework built on top of the Jetpack Compose Runtime. Designed to make it faster and easier to build App Widgets for your home screen or other interfaces.

You are welcome to try it out and share your feedback.

Glance provides a new declarative Kotlin API, similar to the one you use in Jetpack Compose, that helps you build prettier, adaptive App Widgets with less code.

Example Glance “Hello World” Widgets

class GreetingsWidget(private val name: String): GlanceAppWidget() {
    @Composable
    override fun Content(a) {
        Text(text = "Hello $name")}}class GreetingsWidgetReceiver : GlanceAppWidgetReceiver() {

    override val glanceAppWidget = GreetingsWidget("Glance")}Copy the code

The working principle of

Glance provides a base set of composable items to help you create a “at a Glance” experience. Now, we’ll offer more App Widgets. Glance uses the Jetpack Compose Runtime to convert composable items into actual RemoteViews that can be displayed in App Widgets.

△ Glance structure diagram

This means Glance needs to enable Compose and relies on the Runtime, Graphics, and unit interface Compose layers, but it doesn’t have direct interoperability with other existing Jetpack Compose interface elements. However, you can share the status or any other logic in your application to create an intuitive interface.

Alpha is introduced

This initial release introduces major apis that not only enable interoperability with existing RemoteViews, but also help you build App Widgets.

Here’s an overview of what the library provides:

  • Declare your App Widgets with the GlanceAppWidget and GlanceAppWidgetReceiver.

  • Compose your interface with an initial set of Glance composable items: Box, Row, Column, Text, Button, LazyColumn, Image, Spacer.

  • Apply modifiers to composable items using the GlanceModifier method.

  • Handle user interactions with predefined actions:

    • actionStartActivity
    • actionRunCallback
    • actionStartService
    • actionStartBroadcastReceiver
  • Provide parameters for an Action through ActionParameters.

  • Handle different Widgets layouts by defining sizemode. Single, sizemode. Exact, or Sizemode. Responsive.

  • Provide GlanceStateDefinition to preserve the GlanceAppWidget state.

  • Provide local combinations, such as LocalContext, LocalState, LocalGlanceId, and LocalSize.

  • Interoperate with your existing RemoteViews via the AndroidRemoteViews composable.

More features are coming, such as default themes, improved Android Studio support, and more. Stay tuned for the new version.

Note: Although the lowest version of the SDK is 21, Alpha currently supports the lowest compatible version of SDK V23.

Check out the video on Building new Android App Widgets in Android 12 for more details.

Glance Introduction

You can check out the examples on GitHub for a quick start. Glance can also be used with the latest stable version of Android Studio, but you first need to follow the steps in the Jetpack Compose documentation to set up because Glance relies on the Compose Runtime.

You can also check out advanced examples in the AndroidX code base.

By writing your own plug-ins, you can extend the Android Gradle Plugin and customize your build to your project’s needs!

Delta ResponsiveAppWidget. Kt demonstration

Alpha is an important opportunity for you to participate in the development of the API, so please share your feedback and help us understand and further improve your experience! You are welcome to stay tuned for more updates.

Looking forward to your experience!

Please click here to submit your feedback to us, or share your favorite content or questions. Your feedback is very important to us, thank you for your support!