Android Widgets

As you start working with widgets, you will quickly realize that they are stateless. Every call back to paint the widget is invoked and the process dismantled (unless you take reasonable steps to keep this process alive, if that were to be needed). There is no explicit or direct mechanism to ask the widget: "Hey, what did I paint you with the last time around?". This is essentially a valid question if the widget were to have a state. Such a state is typically (indirectly) maintained using a persistence mechansim such as files, shared preferences, SQLLite, or the internet itself.

This item has some example **drafty** source code to illustrate this.

app widget code snippets

BDayWidget code snippets

See the document for: Android Home widgets Research Notes

Honeycomb home screen widgets

How are RemoteViews used for Android Widgets

Latest available remote views

Briefly, here is the core idea of how home screen widgets work (knowing this should make the rest of this section a bit easier to follow). An AppWidgetProvider is a broadcast receiver that gets invoked every once in a while based on a timer interval that you specify in a configuration file. This AppWidgetProvider then loads a RemoteViews instance based on a layout file. This RemoteViews object is then passed to the AppWidgetManager to be displayed on the home screen.

Optionally, you can tell Android that you have an activity that needs to be invoked before placing the widget for the first time on the home screen. This allows the configuration activity to set initialization parameters for the widget.

You also can set up onClick events on the remote views of the widget so that intents can get fired based on those events. These intents then can invoke whatever components necessary, including sending messages to the AppWidgetProvider broadcast receiver.

At a high level, this is all there is to home screen widgets. The rest is the mechanics and variations on each of these basic ideas.

Understand homescreen widgets

what are homescreen widgets?

whats new in honeycomb/ics widgets