Android App Widgets Tutorial
Learn how to give your users fast access to the most important functions of your Android app, right from their home screen, using App Widgets. By Matei Suica.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Android App Widgets Tutorial
35 mins
- Getting started
- App widget anatomy
- User interface
- Resizability and preview
- Refreshing the widget
- Widget customisation
- Create your Widget
- Customizing the User Interface
- Thinking vertically
- Adding buttons
- Run your Widget
- Performing actions
- The RemoteViews class
- Updating the Widget
- Widget configuration
- Managing updates requests
- Update the widget manually
- Communicating via Service
- Making it personal
- Creating a preferences screen
- Know your limits
- Linking preferences to the widget
- Best practices
- Where to go from here
Linking preferences to the widget
It is a good idea to refresh the widget after the user saves the preferences. That’s because the limit might already be exceeded at the moment of adding a new widget. For this reason, write another method at the end of CoffeeLoggerWidgetConfigureActivity to trigger the refresh:
private fun updateWidget() {
val appWidgetManager = AppWidgetManager.getInstance(this)
CoffeeLoggerWidget.updateAppWidget(this, appWidgetManager, appWidgetId)
}
The function retrieves the AppWidgetManager
and triggers an update to the corresponding widget. Call this function in the OnClickListener
after saving the coffee limit to coffeeLoggerPersistence
. It should be before creating the Intent
:
updateWidget()
To launch the configuration screen whenever the user adds a widget, you need to add it to the widget configuration file. With this in mind, open coffee_logger_widget_info.xml and add the following attribute to appwidget-provider:
android:configure="com.raywenderlich.android.coffeelogs.CoffeeLoggerWidgetConfigureActivity"
Build and run, then go to the home screen. Long press the widget and drag it to the “Remove” area. Add another widget as before and check that the configuration screen appears. It should look like this:
Enter a value in the field like 10 and press “Save configuration” to add the widget.
To make the widget react to the limit, add this in CoffeeLoggerWidget inside updateAppWidget*(
, before the last line:
// 1
val limit = coffeeLoggerPersistence.getLimitPref(appWidgetId)
// 2
val background = if (limit <= widgetText.toInt()) R.drawable.background_overlimit
else R.drawable.background
// 3
views.setInt(R.id.widget_layout, "setBackgroundResource", background)
Step by step:
- First, get the limit saved by the user for that widget.
- Decide if the user exceeds the limit of coffee and establish one of the two possible backgrounds: pink or blue.
- Set the background to the widget's root element.
Finally, build and run. After the app opens log more coffees than the limit you set. Let's say your limit was 10: log three Espresso and go back to the home screen. As a result, your widget is now pink:
Best practices
Some final advice before you start adventuring into the world of Widgets:
- Design the smallest Widget size you can. Don't take up screen real-estate if you don't need it. Be aware that the user might resize it into a bigger area.
- Don't refresh the Widget too often because it will drain the battery. On the other hand, don't refresh it too rarely because it won't be useful on the screen.
- Make sure you read the official guidelines for Widget design and follow the recommendations. Revisit them from time to time because things change and things get added.
- Think of Widgets as a shortcut window into your app. Provide the most important information and actions in it.
Where to go from here
Congratulations, you've finished your App Widget! Download the final project using the button at the top or bottom of the tutorial.
You learned how to develop an App widget to track your coffee intake. In summary, some of your new skills are:
- Create a widget layout
- Link a configuration screen
- Communicate via a
Service
... and tie them all together. This is impressive!
You can learn more about App Widgets by checking out the official docs.
For a better understanding of Intents, have a look at the Android Intents Tutorial.
You can create a better user interface for your apps and widgets with more Material Design. Get a little knowledge boost from Android: An Introduction to Material Design.
If you have any questions or comments about Android App Widgets, please join the forum discussion below!