<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gaetano Cammariere on Medium]]></title>
        <description><![CDATA[Stories by Gaetano Cammariere on Medium]]></description>
        <link>https://medium.com/@gaetano.cammariere?source=rss-683fa685e8b6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OWu0mpeKcnCI97Z4lAcK6A.jpeg</url>
            <title>Stories by Gaetano Cammariere on Medium</title>
            <link>https://medium.com/@gaetano.cammariere?source=rss-683fa685e8b6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:43:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gaetano.cammariere/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Unleash Android app widgets with Jetpack Glance]]></title>
            <link>https://medium.com/telepass-digital/revolutionize-android-app-widgets-with-jetpack-glance-bbb1c9f2e9c0?source=rss-683fa685e8b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbb1c9f2e9c0</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[jetpack-glance]]></category>
            <dc:creator><![CDATA[Gaetano Cammariere]]></dc:creator>
            <pubDate>Tue, 10 Oct 2023 10:23:20 GMT</pubDate>
            <atom:updated>2023-10-17T14:25:30.032Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cgPfwxTilvMnPFycswKfrw.jpeg" /></figure><p>In July 2021, <strong>Google </strong>announced the first release of <strong>Jetpack Compose, </strong>permanently changing the way how the Android applications are built. A few months later, <strong>Google </strong>released the first alpha of <strong>Jetpack Glance</strong>, a brand-new platform for developers to create widgets for their applications based on the compose features.</p><p><strong>Jetpack Compose Glance</strong> offers some advantages compared to the classic way to build widget:</p><ul><li>Reduce the boilerplate code</li><li>Use the <strong>compose </strong>syntax to design the UI</li><li>A modern set of API</li></ul><p>An App Widget is a good approach for developers, where it’s possible, to boost user engagement while also giving users a less intrusive way to acquire vital information on the home screen. This is a fantastic approach to significantly enhance the app experience.</p><p>In September 2023, the first stable release of <strong>Jetpack Glance</strong> has been <a href="https://developer.android.com/jetpack/androidx/releases/glance#1.0.0">released</a> and it’s ready for production.</p><p>In this post, we’ll show you how to build a widget that displays images from the internet.</p><h3>App Architecture</h3><p>Here is a quick summary of the key parts of our project before we get into the code:</p><ul><li><strong>Glance Composable Code </strong>describes your UI</li><li>WorkManagerto create tasks to fetch images from our <a href="https://picsum.photos/">API</a></li><li><strong>DataStore </strong>will store the widget state and contains the data that we will use to display the image in the UI</li><li>GlanceAppWidgetManagerfacilitates interactions between the components mentioned above</li><li><strong>Coil</strong>: the library we’ll use as the WorkManager’s client</li></ul><p>This is an example of how these components will interact:</p><ol><li>WorkManager runs a task using <strong>Coil </strong>to get an image from the endpoint</li><li>A <strong>URI referencing the image</strong> is used to save it in the coil cache</li><li>A new state is created using the URI<strong> </strong>and it is sent to the widget through the Datastore</li><li>GlanceAppWidgetManager notifies the state change to the ImageWidget.</li><li>The ImageWidget reads the new state from the Datastore and update the UI reflecting the image change</li></ol><h3>How to configure Gradle for Compose</h3><p>First of all, we need to enable Compose in our <strong>Gradle </strong>configuration</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1595e39eb3af9f7ab04969f7e2634c17/href">https://medium.com/media/1595e39eb3af9f7ab04969f7e2634c17/href</a></iframe><p>and then include <strong>Glance as a dependency</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9b72e324e2cd93a6093d80de0076e6b7/href">https://medium.com/media/9b72e324e2cd93a6093d80de0076e6b7/href</a></iframe><h3>AppWidgetProvider in XML</h3><p>We have to create an <strong>XML file</strong> that represents the configuration of our widget. In this file we will define properties such minWidth, minHeight, description and other properties that you can find detailed <a href="https://developer.android.com/develop/ui/views/appwidgets#AppWidgetProviderInfo">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/585c2cc6c74b3f3ece2eaf35957921f6/href">https://medium.com/media/585c2cc6c74b3f3ece2eaf35957921f6/href</a></iframe><h3>GlanceAppWidget subclass</h3><p>We will create an ImageWidget class that extends <em>GlanceAppWidget</em>. This class overrides a <em>provideGlance</em> function that provides the composable code and it will be translated in <strong><em>RemoteViews</em></strong><em>.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/629de88aa03dfe8799301e4d35a5c4f9/href">https://medium.com/media/629de88aa03dfe8799301e4d35a5c4f9/href</a></iframe><p>The UI basically has <strong>two states</strong>, as seen in the code:</p><ul><li>LOADING: the UI is waiting the image from internet and in this case, we display a loading ring</li><li>SUCCESS: the image is loaded and we can display it</li></ul><p>We will enqueue <strong>a task to the ImageWorker</strong> so that it will get the first image when the widget is attached for the first time.</p><p>By overriding onDelete, we’ll also make sure that all jobs are aborted when the widget is taken down. To handle the user interactions, we will define a RefreshAction.</p><p>This is a <strong>callback </strong>and it will be invoked on the user’s tap on the widget.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2788856a786a5d6a67fee8e9d896f698/href">https://medium.com/media/2788856a786a5d6a67fee8e9d896f698/href</a></iframe><h3>State and GlanceStateDefinition subclass</h3><p>We will create a object called ImageStateDefinition extending GlanceStateDefinition. This class will define where the state of the widget is stored and how it is serialized.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4de82a2ae99baec6eb3f129dafe34b2f/href">https://medium.com/media/4de82a2ae99baec6eb3f129dafe34b2f/href</a></iframe><h3>The worker subclass</h3><p>Using the WorkManager class we will create a worker subclass.</p><p>In this class we will:</p><ol><li>Call the <a href="https://picsum.photos/">API </a>using <strong>Coil </strong>to retrieve a random image with the current size of the widget;</li><li>Store the image in a cache obtaining an <strong>URI </strong>pointing to it;</li><li>Send a new state to the <strong>widget </strong>containing the <strong>URI</strong>;</li></ol><p>All this work is done in the doWork function that will be executed every time a new work will starts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ecfca8e8875ca073cea201cc3f8bf22/href">https://medium.com/media/5ecfca8e8875ca073cea201cc3f8bf22/href</a></iframe><h3>The result</h3><p>The widget will be available in the <strong>Android widget menu</strong> once the project has been launched. This will be the end result once the widget has been pinned to the home screen.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FugTqxEV7Mfg&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DugTqxEV7Mfg&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FugTqxEV7Mfg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=google" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9108409adf3c5464432cd1bbd616caf7/href">https://medium.com/media/9108409adf3c5464432cd1bbd616caf7/href</a></iframe><p>You can find the full code here:</p><p><a href="https://github.com/gaetanocammariere/glance-widget-sample">GitHub - gaetanocammariere/glance-widget-sample</a></p><h3>Useful links</h3><ul><li><a href="https://developer.android.com/develop/ui/views/appwidgets/overview">App Widget documentation</a></li><li><a href="https://developer.android.com/jetpack/compose/glance">Jetpack Glance</a></li><li><a href="https://github.com/android/user-interface-samples">Glance sample repository from google</a></li><li><a href="https://stackoverflow.com/questions/tagged/glance-appwidget">StackOverflow Glance Topic</a></li></ul><p><em>This article was written by Gaetano Cammariere, Android Engineer, and edited by Marta Milasi and Gaetano Matonti, respectively UX Content Lead and Managerial Software Engineer at Telepass. Interested in joining our team? Check out our </em><a href="https://www.telepass.com/it/gruppo/lavora-con-noi"><em>open roles</em></a><em>!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbb1c9f2e9c0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/telepass-digital/revolutionize-android-app-widgets-with-jetpack-glance-bbb1c9f2e9c0">Unleash Android app widgets with Jetpack Glance</a> was originally published in <a href="https://medium.com/telepass-digital">Telepass Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>