A Hands-On Workshop with Jetpack Compose Glance & Gemini -
Activity Instructions & Code Snippets
Slides
Widgets Are So Hot Right Now! - Google Slides
Widget Fever Workshop - Google Slides
Need help??
Join the GDG A/NZ Slack, join the #android channel, there are many developers who can answer your questions! You can find me there with the username @Katie.
Preparation work
- Create your Developer Profile
- Download Android Studio
- Any recent version is fine - stable one is Ladybug
Activity 1 - Set up
- Clone the sample project MotivateMe using git and checkout the
main
branch - Open it in Android Studio
- Sync MotivateMe and run it on an emulator (any emulator is fine)
- Feel free to update the sample data!
Activity 2 - Hello Widget!
- Add the glance dependencies to the app module
build.gradle
file (you can uncomment what has been provided)Click to see example code
- Sync the project
- Create a simple
GlanceAppWidget
that displays some text. You can do this inQuoteWidget.kt
.
Tip: Also update MainActivity to update QuoteWidget on every launch using
QuoteWidget().updateAll(this@MainActivity)
Click to see example code
Click to see example code - imports
Create a simple
GlanceAppWidgetReceiver
that just has an override forglanceAppWidget
, set it to yourQuoteWidget
. You can do this inQuoteWidgetReceiver.kt
Click to see example code
Click to see example code - imports
Update the
AndroidManifest.xml
to include areceiver
that:- Sets
android:name
to your.widget.QuoteWidgetReceiver
- Has
android:exported="true"
- Has an intent filter of
android:name="android.appwidget.action.APPWIDGET_UPDATE"
- Has a
meta-data
object forandroid.appwidget.provider
with the resource of@xml/quote_widget_info
(a basicquote_widget_info.xml
has been provided)Click to see example code
- Sets
Run your app on your emulator
Long press on your home screen to add your new widget!
Activity 3 - Basic configuration
- Modify
QuoteWidget
to change the text or text colour, run your app again to see the change on the widget. See while the app is deploying that the app icon is displayed on the widget. - Modify
QuoteWidget
to add aclickable
modifier withactionStartActivity(intent)
to open theMainActivity
on click of the widget. Run the app and try it out.- Useful reference - User Interaction - Launch an activity
Click to see example code
- Useful reference - User Interaction - Launch an activity
- Modify the
quote_widget_info.xml
to add aninitialLayout
with the default loading layout@layout/glance_default_loading_layout
. Run the app and see the loading layout is now a spinner.Click to see example code
- Modify the
quote_widget_info.xml
to add anandroid:targetCellWidth="4"
andandroid:targetCellHeight="1"
. Also addandroid:resizeMode="horizontal|vertical"
to allow your widget to resize. Run the app and see the default size is now a horizontal rectangle. Try resizing it.Click to see example code
Activity 4 - Set theme & update UI
- Update
QuoteWidget
to include a background modifier to theBox
inQuoteWidgetContent
so it is easier to see the against the wallpaperRun the app and see the colour changeClick to see example code
Note: you may need to remove and re-add the widget.
- Uncomment the code provided in
MotivateMeGlanceColorScheme.kt
Click to see example code
Click to see example code - imports
- Add a
GlanceTheme
implementation inMotivateMeGlanceTheme.kt
that uses the colours fromMotivateMeGlanceColorScheme
- Useful reference - Implement a Glance theme
Click to see example code
Click to see example code - imports
- Useful reference - Implement a Glance theme
Don’t forget the preview!
- Replace
GlanceTheme
withMotivateMeGlanceTheme
inQuoteWidget
. Update the background and the text to use the themed colours: Background =GlanceTheme.colors.widgetBackground
Text =GlanceTheme.colors.primary
Click to see example code
Run the app and see the colour changeClick to see example code - imports
Note: you may need to remove and re-add the widget
- Change the emulator settings from dark to light theme and see the widget colours respond
Activity 5 - Set sample data
- Modify
QuoteWidget
to set thedisplayText
variable from the staticsampleData
as:
val displayText = sampleData.first().quotes.first().text
Click to see example code
2. Run the app, see how the text reflows as the widget changes size & shape.
3. What happens when the text is too long for the space?
Click to see example code
Activity 6 - Add widget configuration & data state
- Update
QuoteWidget
to add thestringPreferencesKey
values for thetopic
andquote
statesClick to see example code
Click to see example code - imports
- Update
QuoteWidgetConfigurationActivity
to set the widget topic & quote as text state valuesClick to see example code
Click to see example code - imports
- Modify
quote_widget_info.xml
to addandroid:widgetFeatures="reconfigurable"
andandroid:configure="dev.motivateme.widget.QuoteWidgetConfigurationActivity"
to allow the widget to be configuredClick to see example code
- Update
QuoteWidget
to read the state and use that value in the widgetClick to see example code
Click to see example code - imports
- Modify
QuoteWidget
to add a button to the widget UI that will randomly select a quote to show.- Useful reference - User Interaction - Run ActionCallback
Click to see example code
Click to see example code - imports
- Useful reference - User Interaction - Run ActionCallback
Activity 7 - Add Gemini
- Open AI Studio and get an API key
- Add Gemini SDK
Click to see example code
- Use Gemini to create a new topic & be able to generate quotes in the main app
Click to see example code
- Save the topic to default shared preferences (the quotes don’t need to be saved - we can generate them fresh each time)
Click to see example code
- Have a go changing the prompts, can you make the motivational quotes more punny?
Activity 8 - Add on demand data refresh using CoroutineWorker
- Update widget configuration to use one of the new topic name, generate a quote and save it in widget state
Click to see example code
- Modify
QuoteWidget
refresh button to call Gemini SDK, read the saved topic name & get a new quote on theIO
threadClick to see example code
Activity 9 - Add periodic data refresh using CoroutineWorker
- Create
QuoteWidgetWorker
to periodically fetch new values from Gemini SDK every 15 minClick to see example code
- Update to
QuoteWidgetReceiver
to set up the periodic data updating withQuoteWidgetWorker
Click to see example code
- Wait for 15 minutes to see if your widget updates! Or, change your device time to trigger the update.
Extensions
Activity 10 (Extension) - Polishing the final result
- Modify the
quote_widget_info.xml
to add anandroid:description="@string/widget_description"
Click to see example code
- Take a screenshot of your widget, trim the size & remove background using any image editor and import it into the app as a drawable with the name
quote_widget_preview.png
Tip: I set a white background on my emulator, then take a screenshot and use photopea.com to crop the image and remove the background
- Modify the
quote_widget_info.xml
to add anandroid:previewImage="@drawable/quote_widget_preview"
.Click to see example code
- Modify
QuoteWidget
to add an error layout if the composition failsClick to see example code
- Run the app and see the widget selection looks much more inviting
Activity 11 (Extension) - Update the GlanceTheme to use the device theme
- Update the
MotivateMeGlanceTheme
to use the device theme- Useful reference - Implement a Glance theme
Click to see example code
- Useful reference - Implement a Glance theme
Activity 12 (Extension) - Respond to the wallpaper
- Update
MotivateMeGlanceTheme
to monitor the wallpaper usingWallpaperManager.OnColorsChangedListener
& pass aBoolean
to the content composable if it should use dark text - Update
QuoteWidget
to enable a transparent background & respond to the wallpaper- Useful reference - WallpaperManager
Click to see example code
- Useful reference - WallpaperManager
Tip: To monitor the wallpaper colours, you will need to increase the minimum sdk to 27
Activity 13 (Extension) - Fine tune Gemini
- Update the Gemini SDK prompt to get it to remove any quote characters from the output
- Can you get a better motivational quote? What other things would you like to display on your home screen?