Android Programming Tutorials

(Romina) #1
A Restaurant In Your Own Home

widget. It is actually a clone of the nine-patch used as the background for


the Toast class, culled from the Android open source project. You are


welcome to use this image or find (or create) another of your choosing.


You will also need an icon, which will go alongside the LunchList name in


Android's list of available widgets. Find some likely icon (32px square or so)


and add it as LunchList/res/drawable/icon.png.


Step #2: Design the App Widget Layout


Next, we need to define a layout for our app widgets. App widgets are


created via layout files, no different than activities, ListView rows, and the


like. Right now, all we want is to show the name of the app widget, inside of


something to serve as the widget's background.


So, create a LunchList/res/layout/widget.xml file with the following content:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/widget_frame"
>
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:textSize="10pt"
android:textColor="#FFFFFFFF"
/>
</RelativeLayout>

Step #3: Add an (Empty) AppWidgetProvider


Next, we need to create an AppWidgetProvider. AppWidgetProvider, a subclass


of BroadcastReceiver, provides the base implementation for an app widget


and gives us lifecycle methods like onUpdate() we can override to add


custom behavior.


316
Free download pdf