Android Widget Start

How to make widget?
What is difference between general Android application and Widget?

First step of Widget

  1. Create class which extends AppWidgetProvider
  2. Create layout of widget(same as Android layout xml)
  3. Create widget definition xml
  4. Edit manifest for Widget

What can we use?

Widget has some limitation to use layout and widget classes.

Layout

  • FrameLayout
  • LinearLayout
  • RelativeLayout
  • GridLayout

UI Components

  • AnalogClock
  • Button
  • Chronometer
  • ImageButton
  • ImageView
  • ProgressBar
  • TextView
  • ViewFlipper
  • ListView
  • GridView
  • StackView
  • AdapterViewFlipper

Sample

First simple widget.

AppWidgetProvider class(MainWidget.java)

public class MainWidget extends AppWidgetProvider 
{
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) 
	{
		// Set remote view
		RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main);
		
		rv.setTextViewText(R.id.word,  "Hello");
		
		// Set view to widget
		ComponentName cn = new ComponentName(context, MainWidget.class);
		appWidgetManager.updateAppWidget(cn, rv);
	}	
}

Layout res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView android:id="@+id/word"
		android:layout_width="fill_parent"
		android:layout_height="match_parent"
		android:textSize="30sp"
		android:textAlignment="center"
		android:gravity="center"
		android:text="Hello"
	/>

</LinearLayout>

Widget definition(res/xml/mainwidget.xml)

Widget definition is widget configuration file.

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

Parameters are very important.
Widget has special size for it. 1 tile is 72dip.
updatePeriodMillis is update time(The minimum is 30 minutes).

AndroidManifest.xml

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <receiver android:name="com.atmarkplant.widgetsample.MainWidget" android:label="MainWidget">
			<intent-filter>
				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
			</intent-filter>
			<meta-data android:name="android.appwidget.provider" android:resource="@xml/mainwidget" />
		</receiver>
        
    </application>

You need receiver tag for Widget.
Name is your class name(with package), label is display name.
Resource is our Widget definition xml file.

We don’t need any Activity in this widget application.

Run

To run this application, “Run” -> “Debug As” -> “Android Application” from eclipse
Install widget and you can see from application or widget list.
Select widget and drag it to your home, and this widget runs.

Result

widget

Ref

Android Developer