How to Celebrate Personal Milestones with an Android Widget Using Jetpack Compose Glance API.

Imagine tracking your progress on quitting smoking or starting a new workout regimen right from your home screen. How much more motivated would you feel?

Ziv Kesten
5 min readApr 13, 2024

The Power of Personal Milestones: Why Build a Widget?

There’s something deeply satisfying about marking the passage of time, especially when it’s tied to personal milestones. Whether it’s quitting smoking, adopting a new habit, or simply tracking progress in a life-changing journey, widgets on your home screen offer a constant, subtle reminder of how far you’ve come. Historically, Android widgets have been a powerful tool for customization and information at a glance. Initially built using XML layouts, they have now evolved with modern development practices, embracing Kotlin and Jetpack Compose.
In this article we will create a “Streak” widget using the new Jetpack Compose Glance API — a fresh and streamlined way to build responsive and efficient widgets.

A Brief History of Android Widgets.

Widgets have been a part of Android’s ecosystem since the early days, allowing users to interact with app functionality directly from the home screen without launching the app itself. Over time, these widgets have transitioned from simple app extensions to complex interactive components that provide real-time data updates and much more.

The introduction of Jetpack Compose has revolutionized Android UI development with its declarative approach to building dynamic UIs efficiently. With the recent introduction of the Jetpack Compose Glance API, developers now have the tools to apply the same intuitive and flexible programming model to widget development.

Step-by-Step Guide to Building Your Streak Widget.

Our widget, aptly named the “Streak Widget,” helps users track the number of days they have adhered to a personal goal. This could be anything from quitting coffee to practicing meditation daily. Let’s dive into the technical implementation.

The first API piece we use is GlanceAppWidgetReceiver
This is where the UI for the widget is provided, we will override the glanceAppWidget to provide the UI.

The StreakWidgetGlance() is the compose UI you will write on your own to provide the UI for your widget, we will get to that shortly

In order to assure the widget is kept updated and can communicate with its hosting app, we will register it in the AndroidManifest file, under the application tag, on the same level as the activity tag.

Necessary XML Configuration.

Although we are Compose aficionados, we still have to get down and dirty with some xml and provide a appwidget-provider file to configure the properties of the widget sizing on the main screen, create this file under res\xml\widget_info.xml

Providing the widget UI

Constructing the widget UI is up to you, you can go crazy with whatever compose UI elements you want, all you have to do eventually is provide it to the glanceAppWidget Above.
The most important thing to remember about building the UI is to extend GlanceAppWidget() and provide whatever composable you want.

Building the widget UI with Glance API in mind

The UI for the widget can be built using Compose just like you do for your app, with some key points to consider.

  1. Using GlanceModifier instead of Modifier: This is necessary because widgets require a more streamlined set of properties tailored for performance and functionality in the widget's limited execution environment.
  2. Passing in resources using the given context rather than using colorResource or stringResource: In Glance API, resources need to be accessed using the context provided. This ensures that the widget can manage its resources independently of the app's lifecycle and maintain proper functionality even when the app is not running.
  3. Limited interactivity and animation: Widgets are not interactive in the same way as a full application. Interactions are generally limited to simple clicks that might launch activities or perform other straightforward tasks. Additionally, animations are not supported in widgets with Glance API, as widgets are meant to be lightweight and non-disruptive on the home screen.

These considerations ensure that the widget performs efficiently and securely, providing necessary information at a glance without excessively consuming system resources.

Building the milestone app

The app itself is, of course not the main story here so i will quickly run through its code which basically just prompts the user to select a date from which to start the streak, save it and update the widget.

The code above is a screen with a title and a button popping a native date picker dialog, and return the selected date, the date can be stored in sharedPreferences, DataStore or any other means of persistence.

Scheduling a daily update for our widget

We can leverage WorkManager to update our widget every 24 hours to increment the number we display on the widget.

We can call this function on the MainActivity where we handle the callback from the MainScreen for selecting a start date.

Displaying the widget on the home screen

Now all we have to do is select our widget from the widgets menu on the home screen and enjoy our new widget!

Conclusion

In conclusion, leveraging the Jetpack Compose Glance API to create a “Streak” widget not only simplifies the development process but also enhances the user experience by making personal milestones visually accessible right on their home screen. As we’ve explored the essentials of widget development — from setting up your XML configuration to implementing the GlanceAppWidgetReceiver and designing a compelling UI — remember that the key to successful widget creation lies in balancing functionality with simplicity. I encourage you to take these insights and embark on your own widget development journey. Please share your experiences, challenges, or any unique designs you create in the comments below.
And as always, dont forget to clap, but only if you think i deverve it!

The full code can be found on Github

--

--

Ziv Kesten
Ziv Kesten

Written by Ziv Kesten

I am a mobile developer, an android enthusiast and a drone lover (Secretly, don’t tell the wife)

No responses yet