Topics What is an App Widget?App Widget frameworkSteps for creating an App WidgetCreating App Widgetconfiguration Activity
What is App Widget?Widgets are an essential aspect of home screencustomization. You can imagine them as "at-a-glance"views of an apps most important data andfunctionality that is accessible right from the usershome screen.
Usage Examples of App Widgets• People can drop widgets onto their homescreen and interact.• Widgets can provide a quick glimpse into fullyfeatured apps, such as showing upcomingcalendar events, or viewing details about asong playing in the background.• Users can also interact with your app throughthe widget, for example pausing or switchingmusic tracks.
AppWidget Framework• AppWidgetProviderInfo object> Describes the metadata for an App Widget, such asthe App Widgets layout, update frequency, and theAppWidgetProvider class.> This should be defined in XML.
AppWidget Framework• AppWidgetProvider class> Defines the basic methods that allow you toprogrammatically interface with the App Widget,based on broadcast events. (AppWidgetProviderclass is a child class of BroadcastReceiver class.)> Through it, you will receive broadcasts when the AppWidget is updated, enabled, disabled and deleted.
AppWidget Framework(cont..)• View layout> Defines the initial layout for the AppWidget, defined in XML.• Additionally, you can implement an App Widgetconfiguration Activity.
Steps for Building an App WidgetDeclare an AppWidgetProvider in the ManifestfileCreate the AppWidgetProviderInfo MetadataXML fileCreate the App Widget Layout XML fileWrite the AppWidgetProvider Class
Declare AppWidgetProvider inManifest• The <receiver> element requires the android:nameattribute, which specifies the AppWidgetProvider• The <intent-filter> element must include an <action>element with the android:name attribute. This attributespecifies that the AppWidgetProvider accepts theACTION_APPWIDGET_UPDATE broadcast.• The <meta-data> element specifies the location
Create AppWidgetProviderInfo Metadata• Define the AppWidgetProviderInfo object in an XMLresource using a single <appwidget-provider> elementand save it in the projects res/xml/ folder.> This file is referenced from the manifest file• Defines the essential qualities of an App Widget, suchas its minimum layout dimensions, its initial layoutresource, how often to update the AppWidget, and(optionally) a configuration Activity to launch at create time.
Create App Widget Layout• App Widget layouts are based on RemoteViews,which do not support every kind of layout orview widget.• A RemoteViews object (and, consequently, anApp Widget) can support the following layoutsand Widget classes
Write AppWidgetProvider ClassThe AppWidgetProvider class extends BroadcastReceiver asa convenience class to handle the App Widget broadcasts• Methods to override> onUpdate(Context, AppWidgetManager, int) - calledwhen each App Widget is added to a host (unless youuse a configuration Activity), Typically the onlymethod that needs to be present> onDeleted(Context, int)> onEnabled(Context)>onDisabled(Context)> onReceive(Context, Intent)
Why App Widget Configuration Activity?• If you would like the user to configure settings when he orshe adds a new App Widget, you can create an App Widgetconfiguration Activity.• This Activity will be automatically launched by the AppWidget host and allows the user to configure available settingsfor the App Widget at create-time, such as the App Widgetcolour, size, update period or other functionality settings.
Declare it in Manifest File• The configuration Activity should be declared as a normalActivity in the Android manifest file.• However, it will be launched by the App Widget host withthe ACTION_APPWIDGET_CONFIGURE action, so the Activityneeds to accept this Intent<activity android:name=".ExampleAppWidgetConfigure"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_CONFIGURE" /></intent-filter></activity>
Declare it in Metadata Config file• Also, the Activity must be declared in theAppWidgetProviderInfo XML file, with the android:configureattribute<appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"...android:configure="com.example.android.ExampleAppWidgetConfigure"... ></appwidget-provider>