Android Application Widgets

Home-screen widgets on Android are one of the features that really makes the platform stand out from the crowd. The ability to have dynamically updated information directly available to the user without starting an application is a truly great feature. It’s one of those things that I keep expecting Apple to add to their platform and has yet to appear.

There seems to be a bit of a mystique around the creation and operation of widgets. In this post, I want to take a quick look at how they work from a slightly simplistic architectural perspective so that when you build one it all hangs together and makes sense.

The most significant thing about widgets is that there are actually two machine processes involved. The first is the Home screen process which is the application the user interacts with to start other applications. The second is the widget process which sits in the background and sends updates to the Home screen at intervals of 30 minutes or greater.

Simplified Widget Architecture

Once you understand the basic architecture, the only thing left to understand is the communication between the widget code (running in a class which extends AppWidgetProvider) and the widget which is displayed on the Home screen. The key to all this is a neat little class called RemoteViews.

RemoteViews are created by the AppWidgetProvider class in response to an onUpdate() message from the operating system. Each RemoteViews object  contains a reference to a layout resource which describes the visual appearance of the widget. In addition, the RemoteViews object has a number of methods such as setTextViewText() or setTextColor() which are called before the RemoteViews object is dispatched to the Home screen. Each of these actually queues up an action to be performed when the RemoteViews object is processed. When the RemoteViews object arrives at the Home screen controller, the visual layout is rendered and then each of the queued actions is performed adding the dynamic content to the widget view.

You need to be aware that the RemoteViews method can only support a limited set of on-screen controls (confusingly also known as widgets!). The supported widgets are: AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar, TextView. Whilst that may seem limiting, you can actually do almost anything by creating the controls in the background and then rendering them as an image. One of these days, I’ll write something about the dynamic dials I’ve created for widgets using that tecnique. All I need is time…

If you want to know more about building widgets, Learning Tree has a great new course Building Android™ Applications: Extended Features.

Mike Way

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.