Better Programming

Advice for programmers.

Follow publication

Build Android App Widgets Using Jetpack Glance

Build a Bitcoin price widget with a refresh button

Enes Zor
Better Programming
Published in
3 min readJan 15, 2022

--

Photo by Đức Trịnh on Unsplash

It is been a long time since my last post. This time, I am here to introduce Jetpack Glance for App Widgets to you. Let's get started.

Jetpack Glance is a compose style framework that allows to develop remote Android views.

You can build a fancy, modern, and responsible app widgets with minimal code and Declarative Kotlin API.

Jetpack Glance is in still alpha with Version 1.0.0-alpha01.

To add Glance to your project, you need to add Google Maven and dependency in your build.gradle.

You might wonder Glance works? Just with a few sentences, it provides compose view compatibility with remote views. Do note that Glance has its own set of composables that are not interoperable with androidx.compose.

Though Glance’s own set of composable doesn’t comprise of all of the Jetpack Compose components yet. I think that they will appear further stable releases. Alright then, let's sweat our hands!

Implementation

At this point, I will start to explain how to create a bitcoin widget step by step with code implementation. The idea of this Bitcoin widget is to allow the user to view the daily bitcoin price changes.

Furthermore, they can click the refresh button to the fetch latest value from a local data source. I said local source since we put the latest value into local if it is not expired.

1. Create AppWidgetProvider in XML

To start off, create an app widget XML into resources.

This XML file is your identity of the specific widget. You can add essential attributes into it such as minWidth, minHeight and updateFrequency etc. For more attributes, please check the Android official documentation.

app_widget_info.xml

2. Create GlanceAppWidget subclass

This is our UI layer which communicates with AppWidgetManager.

When you call its update method, composition will be started and the composed views will be translated into RemoteViews.

For composition, you need to override Content methods like below:

3. Create Your Widget’s Composable View

By the way, I’ve separated my compose view into another BitcoinWidget.kt file as shown below:

3. Create a subclass of GlanceAppWidgetReceiver

GlanceAppWidgetReceiver is a class derived from the AppWidgetProvider to generate remote views.

You need to override glanceAppWidget attribute to create your widget receiver.

A lot of things are happening in the above MarketWidgetReceiver class.

  • The onUpdate method will be called when the widget appears for the first time on your screen.
  • There is also an observeData function called the onUpdate method. The observeData method launches the coroutine to fetch data from local. Once we’ve got the latest Bitcoin price data, we’d use the updateAppWidgetState to display the latest values from the Preferences and invoke the GlanceAppWidget’s update method for refreshing the view.
  • But for the widget to update we need a way to listen to broadcast events. This is where onReceive method comes into the picture with a new MarketRefreshCallback action broadcast.

4. Create a Composable UI for widget refresh

Before we get into the callback class implementation, let’s create a BitcoinWidgetHeader view that consists of a refresh button (which will eventually trigger the callback):

The above code is self-explanatory.

5. Setup the ActionCallBack for the App Widget

Now let’s create an ActionCallback. Since we’re looking to update the widget on button click — we’ve created out ActionCallback and overriden the onRun method.

Inside onRun method, we created an intent to sendBroadcast. This broadcast will inform MarketWidgetReceiver’s onReceive method. Eventually, we get this broadcast via intent and observe our latest data.

6. Update the AndroidManifest.xml file

Next up, update the Manifest file by adding the MarketWidgetReceiver class. And, do not forget to add an app widget provider as well like its shown below:

Here is the final preview with all of the functionality in action:

Source Code

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Enes Zor
Enes Zor

Written by Enes Zor

Android Developer @Trendyol

Responses (3)