Android Widgets

1. Android Widgets

1.1. Overview about AppWidgets

Widgets are little applications which can be placed on a widget host, typically the home screen or the lock screen of your Android device.

A widget runs as part of the process of its host. This requires that the widget preserves the permissions of their application.

Widget use RemoteViews to create their user interface. A RemoteView can be executed by another process with the same permissions as the original application. This way the widget runs with the permissions of its defining application.

The user interface for a Widget is defined by a broadcast receiver. This receiver inflates its layout into an object of type RemoteViews. This object is delivered to Android, which hands it over the home screen application.

1.2. Steps to create a Widget

To create a widget, you:

  • Define a layout file
  • Create an XML file (AppWidgetProviderInfo) which describes the properties of the widget, e.g. size or the fixed update frequency.
  • Create a BroadcastReceiver which is used to build the user interface of the widget.
  • Enter the Widget configuration in the AndroidManifest.xml file.
  • Optional you can specify a configuration activity which is called once a new instance of the widget is added to the widget host.

    2. Creating the Broadcast receiver for the widget

    2.1. Create and configure widget

    To register a widget, you create a broadcast receiver with an intent filter for theandroid.appwidget.action.APPWIDGET_UPDATE action.

     <receiver
           android:icon="@drawable/icon"
           android:label="Example Widget"
           android:name="MyWidgetProvider" >
           <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
           </intent-filter>
    
           <meta-data
              android:name="android.appwidget.provider"
              android:resource="@xml/widget_info" />
    </receiver>

    The receiver can get a label and icon assigned. These are used in the list of available widgets in the Android launcher.

    You also specify the meta-data for the widget via theandroid:name="android.appwidget.provider attribute. The configuration file referred by this metadata contains the configuration settings for the widget. It contains, for example, the update interface, the size and the initial layout of the widget.

    <?xml version="1.0" encoding="utf-8"?>
    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
        android:initialLayout="@layout/widget_layout"
        android:minHeight="72dp"
        android:minWidth="146dp"
        android:updatePeriodMillis="1800000" >
    
    </appwidget-provider>

    2.2. Available views and layouts

    A widget is restricted in the View classes it can use. As layouts you can use the FrameLayout,LinearLayout and RelativeLayout classes. As views you can use AnalogClock, Button,Chromometer, ImageButton, ImageView, ProgressBar and TextView.

    As of Android 3.0 more views are available: GridView, ListView, StackView, ViewFlipperand AdapterViewFlipper. These adapter views require that you define a collection view widgetwhich is described later in this {textselfreference}.

    The only interaction that is possible with the views of a widget is via an OnClickListener event. This OnClickListener can be registered on a widget and is triggered by the user.

    2.3. AppWidgetProvider

    Your BroadcastReceiver implementation typically extends the AppWidgetProvider class.

    The AppWidgetProvider class implements the onReceive() method, extracts the required information and calls the following widget life cycle methods.

    As you can add several instances of a widget to the home screen, you have life cycle methods which are called only for the first instance added / removed to the home screen and others which are called for every instance of your widget.

    Table 1. Life cycle method
    Method Description

    onEnabled()

    Called the first time an instance of your widget is added to the home screen.

    onDisabled()

    Called once the last instance of your widget is removed from the home screen.

    onUpdate()

    Called for every update of the widget. Contains the ids of appWidgetIds for which an update is needed. Note that this may be all of the AppWidget instances for this provider, or just a subset of them, as stated in the method’s JavaDoc. For example, if more than one widget is added to the home screen, only the last one changes (until reinstall).

    onDeleted()

    Widget instance is removed from the home screen.

    2.4. Receiver and asynchronous processing

    A widget has the same runtime restrictions as a normal broadcast receiver, i.e., it has only 5 seconds to finish its processing.

    A receive (widget) should therefore perform time consuming operations in a service and perform the update of the widgets from the service.

Leave a Reply

Your email address will not be published. Required fields are marked *