Chapters

Hide chapters

Jetpack Compose by Tutorials

Second Edition · Android 13 · Kotlin 1.7 · Android Studio Dolphin

Section VI: Appendices

Section 6: 1 chapter
Show chapters Hide chapters

16. Creating Widgets Using Jetpack Glance
Written by Denis Buketa

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

If you want to extend your app’s functionality beyond the app itself, App Widgets are the best way to do it. They allow you to provide some features of your app as at-a-glance views that live in your user’s home screen.

According to Google’s blog announcement for Jetpack Glance, 84% of users use at least one widget. That gives you a sense of how important it is to at least be familiar with the basics of building app widgets.

In this chapter, you’ll learn how simple it is to build app widgets using Jetpack Glance.

You’ll learn:

  • What is Jetpack Glance.
  • How to define essential characteristics of your app widget using AppWidgetProvider.
  • How to use GlanceAppWidgetReceiver to instantiate your app widget and update it.
  • How to create UI layouts using GlanceAppWidget.
  • How to handle actions in the widget.

Note: If you want to check Google’s announcement of Jetpack Glance, please refer to the official Google blog: https://android-developers.googleblog.com/2021/12/announcing-jetpack-glance-alpha-for-app.html.

Introducing Jetpack Glance

Jetpack Glance is a new framework that allows you to build app widgets using the same declarative APIs that you are used to with Jetpack Compose.

Beside using the similar APIs, it uses Jetpack Compose Runtime to translate a Composable into a RemoteView, which it then displays in an app widget. It also depends on Jetpack Compose Graphics and UI layers that you covered in the very first chapter of this book.

Glance Glance-appwidget XML RemoteViews GlanceAppWidgetReceiver Compose Runtime ...
Glance Structure

Keep in mind that these dependencies mean that Glance requires for you to enable Compose in your project, but it’s not directly interoperable with other Jetpack Compose UI elements. Because of that, you’ll notice that in this chapter you’ll use GlanceModifier instead of Modifier and some other composables will be imported from androidx.glance package instead of androidx.compose package.

Defining Essential Characteristics of Your App Widget

To follow along with the code examples, open this chapter’s starter project in Android Studio and select Open an existing project.

Nuxjoyyoxq Wztueb
Pacnadsujt Ftfuer

Creating the Hello Glance Widget

In this section you’ll create a Hello Glance widget so that you get one step closer to displaying your widget on screen.

Displaying the Widget on the Home Screen

The final step before you can see your widget on the home screen is to create AppWidgetProvider.

Fikcuz Onmuiy um Loomwvoy
Xazcar Utnuup ar Heabwdaq

Punmiq Nfodoud
Qozmic Zkohius

Rihceh up Qico Ctyoep
Hedboz at Vufu Mdqauj

Adding Subreddits to the Widget

Before you start implementing the rest of the UI, it is important to mention that @Preview still doesn’t work for Jetpack Glance. You’ll be coding the UI for the widget and then building the project later to see it.

Tovwoyqokk Rulsak
Xasziryoyz Xuqmaz

Handling Widget Actions

First, let’s handle the onCheckedChange() action in Switch(). When working with widgets, you handle user actions using the ActionCallback interface.

Connecting the Widget With the JetReddit App

At the beginning of the chapter, it was mentioned that the JetReddit app now stores states of the switches in preferences.

Qoyxih jefbiqtik zewd JinLabyad
Rimmel kihkurvaj niyx JupFugdun

Key Points

  • Jetpack Glance is a new framework that allows you to build app widgets using declarative APIs that you are used to with Jetpack Compose.
  • To define widget’s essential characteristic, you need to create a appwidget-provider in res/xml folder.
  • You use GlanceAppWidget to define the widget UI and to communicate with the AppWidgetManager.
  • You need to create GlanceAppWidgetReceiver and define in the AndroidManifest.xml for your app to be able to create widgets.
  • If you want to handle widget actions, you’ll use ActionCallback.
  • ActionParameters enables you to pass data between app widget and ActionCallback.
  • To make your app widget stateful, you can update its state with updateAppWidgetState().
  • The onUpdate() method in GlanceAppWidgetReceiver() allows you to update the widget state once the user creates it.

Where to Go From here?

Congratulations, you just completed the Creating Widgets Using Jetpack Glance chapter!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now