Chapter 14. App Widgets

In Android, the idea of showing mini application views embedded in other applications, the most common case being that of the home screen, is a very important and useful feature. These are called app widgets, or widgets for short. These widgets not only provide a small window into an easily accessible view, but also can receive updates and thus provide a more dynamic experience to your application.

Using Content Providers Through Widgets

As mentioned before, content providers make the most sense when you want to expose the data to other applications. It is a good practice to always think of your application as part of a larger Android ecosystem and, as such, a potential provider of useful data to other applications.

To demonstrate how content providers can be useful, we’ll create a home screen widget. We’re not using the term widget here as a synonym for Android’s View class, but as a useful embedded service offered by the home screen.

Android typically ships with a few home screen widgets. You can access them by going to your home screen, long-pressing on it to pull up an Add to Home Screen dialog, and choosing Widgets. Widgets that come with Android include Alarm Clock, Picture Frame, Power Controls, Music, and Search. Our goal is to create our own Yamba widget that the user will be able to add to the home screen.

The Yamba widget will be simple, displaying just the latest status update. To create it, we’ll make a new YambaWidget class that subclasses AppWidgetProviderInfo ...

Get Learning Android, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.