aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Mobile
  • Programming

Experimenting With Jetpack Glance

  • aster.cloud
  • September 7, 2022
  • 3 minute read

We’re happy to announce that the new Jetpack Glance framework, which lets you build app widgets and wear tiles with Jetpack Compose-like code, has reached alpha-04.

We also just released a standalone experimental repository to supplement Jetpack Glance with tools that are commonly required for development but not yet available.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

Important: This project is independent from the AndroidX Glance repository and development. The <a class="au ku" href="https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceRemoteViews" target="_blank" rel="noopener ugc nofollow"><em class="iy">GlanceRemoteViews</em></a> API and any of the features in the experimental repository may, in the future, change or become obsolete (and therefore likely deprecated).

Why can’t these be in AndroidX?

We want to iterate, explore and experiment with some new APIs and tooling more freely, without adding overhead to the main API and avoid API commitments. In addition, some of these features might not be allowed in AndroidX.

Note: this repository follows the Accompanist pattern but in a much more narrow scope (read more about it here)

Glance Experimental Tools

The initial idea is to use the new experimental <a class="au ku" href="https://developer.android.com/reference/kotlin/androidx/glance/appwidget/GlanceRemoteViews" target="_blank" rel="noopener ugc nofollow">GlanceRemoteViews</a> API to enable new features together with Jetpack Compose. Let’s explore the first available modules:

AppWidget Host composable

You can use the AppWidgetHost composable to host and display RemoteViews inside your app. This module, together with the new GlanceRemoteViews API, powers the other modules with a simple API that displays a RemoteViews object, decoupled from AppWidgets and Glance.

Let’s take a look at an example:

/**
* Copyright 2022 Google LLC.
* SPDX-License-Identifier: Apache-2.0
*/

@Composable
fun MyScreen(glanceAppWidget: GlanceAppWidget) {
    val state = rememberAppWidgetHostState()
    // the available size for the widget
    val size = DpSize(200.dp, 200.dp)

    if (previewHostState.isReady) {
        // When the host is ready, generate the remote views.
        LaunchedEffect(previewHostState.value) {
            state.updateAppWidget(
                // Extension method that uses GlanceRemoteViews
                glanceAppWidget.compose(context, size)
            )
        }
    }
    AppWidgetHost(
        modifier = Modifier.fillMaxSize(),
        widgetSize = size,
        state = state
    )
}

The AppWidgetHostState updates the host with the given RemoteViews. Here is where we can use the new GlanceRemoteViews API to generate them from a Glance-composable function.

Read More  Google I/O 2019 | Smart Home 101: How to Develop for the Connected Home

Read more about this module here.

Note: You can directly use RemoteViews without Glance, as this module doesn’t depend on glance-appwidget.

Viewer for glance-appwidget

When it comes to testing and debugging AppWidgets, a major pain point has been the laborious process required for deploying a single iteration:

  1. Build your project. 😃
  2. Launch the app. 🙂
  3. Close the app. 😐
  4. Remove and add the app widget in the launcher 😕 (or use this workaround ☹️)
  5. Done! 🥵

This process is quite time-consuming even with the workaround. And since there isn’t yet a preview for Glance, making changes in your composables without seeing the result immediately might be tricky.

… wait a minute, “Live Edit” works with Glance?

demo of code changes immediately reflect in live-preview
Code changes on the left, gets reflected in the device on the right

That’s right: no more waiting for the app to build and launch. By extending the <a class="au ku" href="https://github.com/google/glance-experimental-tools/blob/main/appwidget-preview/src/main/java/com/google/android/glance/tools/preview/AppWidgetPreviewActivity.kt" target="_blank" rel="noopener ugc nofollow">GlancePreviewActivity</a> for your debug builds, you can speed up UI iterations. The extended activity displays your app’s widgets inside your own app (instead of the launcher), providing much faster previews and enabling Apply Changes and Live Edit features from Android Studio.

The glance-appwidget viewer does not rely on the <a class="au ku" href="https://developer.android.com/reference/android/appwidget/AppWidgetManager" target="_blank" rel="noopener ugc nofollow">AppWidgetManager</a>; instead, it uses the AppWidgetHostView directly to skip the BroadcastReceiver mechanism to render the RemoteViews inside the app’s activity (there are some limitations).

Using this viewer, together with Compose and Live Edit, we can achieve (in most situations) a real-time update mechanism, displaying code changes in your Glance composables nearly instantaneously.

Not only that, the viewer activity enables other available developer tools such as the Layout Inspector.

Read More  Android Dev Summit 2019 | What's New In Android Studio Design Tools
screenshot of layout inspector with the app widget preview
Inspecting a GlanceAppWidget with Layout-Inspector

In addition, the viewer activity provides the following features:

  • 🔀 Switching between different widgets
  • 🔎 Resizing and reflecting widget available space changes (respecting the SizeMode)
  • ℹ️ Highlighting missing appwidget meta-data
  • 📌 Requesting the launcher to pin the current preview
  • 🖼️ Extracting and sharing of the current preview (android:previewImage)
showcase of the preview activity different options (like resize, info, etc..)

Read more in the additional features section and check the setup section of the project.

Using Compose @Preview

The Viewer activity provides useful functionality, but it requires additional setup and material dependencies. If you only want to display previews of your GlanceAppWidgets with the in-build Preview mechanism in Android Studio, it is also possible using the GlanceAppWidgetHostPreview.

/**
* Copyright 2022 Google LLC.
* SPDX-License-Identifier: Apache-2.0
*/

@OptIn(ExperimentalGlanceRemoteViewsApi::class)
@Preview
@Composable
fun SampleGlanceWidgetPreview() {
    // The size of the widget
    val displaySize = DpSize(200.dp, 200.dp)
    // Provide a state depending on the GlanceAppWidget state definition
    val state = preferencesOf(SampleGlanceWidget.countKey to 2)

    GlanceAppWidgetHostPreview(
        modifier = Modifier.fillMaxSize(),
        glanceAppWidget = SampleGlanceWidget, // The actual instance
        state = state,
        displaySize = displaySize,
    )
}

Note: while the preview will render in Android Studio, the RemoteViews won’t. You must always run it in a device. Better support for this will be provided later on.

GlanceAppWidget Configuration composable

This GlanceAppWidget Configuration composable module offers an opinionated composable to use in your configuration activity for your app widgets. In a nutshell, it builds on top of the Material3 Scaffold to display and handle the appwidget activity configuration logic for Glance.

demo showcasing the configuration activity when placing an app widget

This composable is great for:

  • Making the transition between the launcher and the configuration screen smoother and aligned (Material You!)
  • Displaying a realistic preview of the appwidget (preserves the size) to help users configure it
  • Enabling the GlanceAppWidget to reflect state changes without actually applying them
Read More  Custom Canvas Animations In Jetpack Compose ✨

Check out this guide to start using it!

By Marcel Pintó
Source Medium


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

aster.cloud

Related Topics
  • AndroidX
  • Jetpack
  • Jetpack Compose
  • Jetpack Glance
You May Also Like
View Post
  • Architecture
  • Data
  • Engineering
  • People
  • Programming
  • Software Engineering
  • Technology
  • Work & Jobs

Predictions: Top 25 Careers Likely In High Demand In The Future

  • June 6, 2023
View Post
  • Programming
  • Software Engineering
  • Technology

Build a Python App to Alert You When Asteroids Are Close to Earth

  • May 22, 2023
View Post
  • Programming

Illuminating Interactions: Visual State In Jetpack Compose

  • May 20, 2023
View Post
  • Computing
  • Data
  • Programming
  • Software
  • Software Engineering

The Top 10 Data Interchange Or Data Exchange Format Used Today

  • May 11, 2023
View Post
  • Gears
  • Mobile
  • Technology

Apple Watch Pride Edition Celebrates The LGBTQ+ Community

  • May 10, 2023
View Post
  • Architecture
  • Programming
  • Public Cloud

From Receipts To Riches: Save Money W/ Google Cloud & Supermarket Bills – Part 1

  • May 8, 2023
View Post
  • Programming
  • Public Cloud

3 New Ways To Authorize Users To Your Private Workloads On Cloud Run

  • May 4, 2023
View Post
  • Programming
  • Public Cloud

Buffer HTTP Requests With Cloud Tasks

  • May 4, 2023

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.