Android 101: How to create a StackView widget

Widgets are a great tool for a user to customize his home screen. It is a great way to enhance an app's engagement. A widget can provide a lot of information at a glance and offer shortcuts for common actions.

Honeycomb introduced widgets with collections. As the name implies, they are able to display several items. There are several types of widgets with collections:

  • ListView: a vertical scrolling list of items (e.g. Gmail widget).
  • GridView: a scrolling list with two columns of items (e.g. bookmarks widget).
  • StackView: a stacked card view, where the front item can be flipped to give room for the item after it (e.g. Market widget).
  • AdapterViewFlipper: animates between views, only showing one at a time.

I will explain how we built the widget for the Honeybuzz application. It is a StackView and displays Buzz-like cards with a user picture and a portion of the Buzz text.

Add the widget to the manifest

The widget needs to be declared in the application's manifest. Because we are using a StackView widget, we specify both the widget provider and the widget service.

    <!-- StackWidget Provider -->
    <receiver android:name="StackWidgetProvider">
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            android:resource="@xml/stackwidgetinfo" />
    <!-- StackWidget Service -->
    <service android:name="StackWidgetService"
        android:exported="false" />

How to create the widget provider

For the widget provider, you need to specify the widget provider info and create the widget provider class implementation.

The widget provider info is just an XML file that you create in the res\xml folder of your application. Here is the file from the Honeybuzz application:

<?xml version="1.0" encoding="utf-8"?>

Most of the attributes are self explanatory. The updatePeriodMillis attribute defines how often it requests an update from the provider class. The minimum possible is 15 minutes, but you should update as little as possible in order to save battery life.

To determine the widget size you can use the following formula (given in the Android Developers site):

  • (number of cells * 74) - 2

So if you want a 3 by 3 widget like in the Honeybuzz application you get for each side:

  • (3 * 74) - 2 = 220

A layout widget is comparable to a normal layout for an Activity, but it is much more restrictive. They are based on RemoteViews and both the layout classes and the views that you can use are limited (especially before Honeycomb, where scrolling views were not available for widgets).

A layout for a StackView widget can be as simple as the following:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android=""
    <StackView xmlns:android=""
        android:loopViews="true" />
    <TextView xmlns:android=""
        android:textSize="16sp" />

Now for the implementation of the widget provider class:

public class StackWidgetProvider extends AppWidgetProvider {
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);

    public void onDisabled(Context context) {

    public void onEnabled(Context context) {

    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int i = 0; i < appWidgetIds.length; ++i) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.stackwidget);

            // set intent for widget service that will create the views
            Intent serviceIntent = new Intent(context, StackWidgetService.class);
            serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            serviceIntent.setData(Uri.parse(serviceIntent.toUri(Intent.URI_INTENT_SCHEME))); // embed extras so they don't get ignored
            remoteViews.setRemoteAdapter(appWidgetIds[i],, serviceIntent);
            // set intent for item click (opens main activity)
            Intent viewIntent = new Intent(context, HoneybuzzListActivity.class);
            viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
            PendingIntent viewPendingIntent = PendingIntent.getActivity(context, 0, viewIntent, 0);
            remoteViews.setPendingIntentTemplate(, viewPendingIntent);
            // update widget
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        super.onUpdate(context, appWidgetManager, appWidgetIds);

In the onUpdate method we iterate through all the widget instances and we add Intents for the service that will create each widget view and also for an item's click.

Create the widget service and the view for each item

In a StackView widget you need a separate layout that will be used to display a single item in the collection. You should know how to create one by now, but here is the one used in the Honeybuzz widget as an example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    <LinearLayout xmlns:android=""
          <LinearLayout xmlns:android=""
             <ImageView android:id="@+id/stackWidgetItemPicture"
            <TextView android:id="@+id/stackWidgetItemUsername"
         <TextView android:id="@+id/stackWidgetItemContent"

The widget service has to specify the views factory, which is responsible for creating a view for each item in the collection.

public class StackWidgetService extends RemoteViewsService {
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
    private final ImageDownloader imageDownloader = new ImageDownloader();
    private List<Buzz> mBuzzes = new ArrayList<Buzz>();
    private Context mContext;
    private int mAppWidgetId;

    public StackRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

    public void onCreate() {

    public void onDestroy() {

    public int getCount() {
        return mBuzzes.size();

    public RemoteViews getViewAt(int position) {
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stackwidget_item);
        if (position <= getCount()) {
            Buzz buzz = mBuzzes.get(position);
            if (buzz.picture != null) {
                try {
                    Bitmap picture = imageDownloader.downloadBitmap(buzz.picture, 100, 100, 70);
                    rv.setImageViewBitmap(, picture);
                catch(Exception e) {
                    Logging.e("Error reading picture file", e);
            if (!buzz.username.isEmpty()) {
                rv.setTextViewText(, buzz.username);
            rv.setTextViewText(, Html.fromHtml(buzz.content));
            // store the buzz ID in the extras so the main activity can use it
            Bundle extras = new Bundle();
            Intent fillInIntent = new Intent();
            rv.setOnClickFillInIntent(, fillInIntent);
        return rv;

    public RemoteViews getLoadingView() {
        return null;

    public int getViewTypeCount() {
        return 1;

    public long getItemId(int position) {
        return position;

    public boolean hasStableIds() {
        return true;

    public void onDataSetChanged() {
        mBuzzes = Buzz.getBuzzes(, mContext);

The important tidbits:

  • StackWidgetService: override the onGetViewFactory method so we can return our own views factory.
  • StackRemoteViewsFactory: will handle the views for each item in the collection.
    • onDataSetChanged: loads the necessary data that will be displayed in the widget.
    • onDestroy: make sure to destroy any objects that are no longer needed.
    • getViewAt: must return a view for the item at the specified position. We create a new view from the layout file specified previously, then we get the appropriate Buzz object which we use to fill the view with information. At the end we make sure the view has the on-click Intent, so that when it gets clicked, it will open the Honeybuzz application and load the correct Buzz object.

How to create a preview image for your widget

A preview image will be shown when the user is selecting the widget to add from the gallery and will help a user understand how the widget looks like and what it does. If you don't provide a preview image for your widget, your application's icon will be used instead.

Thankfully, the Android emulator and the Android SDK come with an application to generate a preview image from a widget. The application is called "Widget Preview" and you can either use it from the emulator or copy it to your own device and run it from there. You will find the code for the application in your SDK folder at android-sdk\samples\android-11\WidgetPreview. You can open the project, build it and copy it to your device. The application is straightforward to use and very useful. After generating the image, you need to include it in the drawables of your project and specify it in the widget provider info resource (see the provider info above for an example).

More resources

Nuno Freitas
Posted by Nuno Freitas on December 12, 2011

Related articles