Google releases Jetpack Glance Alpha, making it easier to build Android 12 widgets

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.

join us on telegram

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

AndroidRemoteViews

RemoteViews

△ 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

Write your own plugin, you can extend Android Gradle Plugin and customize your build according to your project needs!

ResponsiveAppWidget.kt demo

ResponsiveAppWidget.kt

Leave a Comment