# Widgets

# Manifest Declaration -

Declare the AppWidgetProvider class in your application's AndroidManifest.xml file. For example:

<receiver android:name="ExampleAppWidgetProvider" >
    <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<meta-data android:name="android.appwidget.provider"
           android:resource="@xml/example_appwidget_info" />

# Metadata

Add the AppWidgetProviderInfo metadata in res/xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

# AppWidgetProvider Class

The most important AppWidgetProvider callback is onUpdate(). It is called everytime an appwidget is added.

public class ExampleAppWidgetProvider extends AppWidgetProvider {

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i=0; i<N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, ExampleActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);

onAppWidgetOptionsChanged() is called when the widget is placed or resized.

onDeleted(Context, int[]) is called when the widget is deleted.

# Two widgets with different layouts declaration

  1. Declare two receivers in a manifest file:
    android:label="UVMate Widget 1x1">
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

        android:resource="@xml/widget_1x1" />
    android:label="UVMate Widget 2x2">
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

        android:resource="@xml/widget_2x2" />

  • Create two layouts
      1. `@xml/widget_1x1` 1. `@xml/widget_2x2`
  • 1. Declare the subclass `UVMateWidget2x2` from the `UVMateWidget` class with extended behavior:
    package au.com.aershov.uvmate;
    import android.content.Context;
    import android.widget.RemoteViews;
    public class UVMateWidget2x2 extends UVMateWidget {
        public RemoteViews getRemoteViews(Context context, int minWidth,
                                          int minHeight) {
            return new RemoteViews(context.getPackageName(), R.layout.widget_2x2);

    # Create/Integrate Basic Widget using Android Studio

    Latest Android Studio will create & integrate a Basic Widget to your Application in 2 steps.

    # Right on your Application ==> New ==> Widget ==> App Widget


    enter image description here

    It will show a Screen like below & fill the fields

    enter image description here

    Its Done.

    It will create & integrate a basic HelloWorld Widget(Including Layout File , Meta Data File , Declaration in Manifest File etc.) to your Application.

    # Remarks