An Amendment to an Android Widget Tutorial
I’m trying to make an Android widget, and Google turned up a pretty straightforward and well-presented tutorial that I decided to follow to get a feel for things.
The tutorial was fairly easy to follow aside from the text boxes that were too narrow for the code samples, but it had some bits that were either out of date or, more likely, just wrong. So without further ado, I present some errata:
In step 4, you edit the widget’s
main.xml
file, which the tutorial doesn’t explain should be located inres/layout
in your project. Most of it works, but:- The
android:id
attribute of theLinearLayout
tag is wrong, which is unfortunate. The tutorial mentions in step 3 that Eclipse will complain aboutR.id.widget_textview
and that it will be created shortly. It also mentions in step 4 that “the important thing is the id of our TextView, which we used earlier in the WatchWidget class.” But then it still gets it wrong. Oh well. The value of theandroid:id
attribute should actually be"@+id/widget_textview
. - I’m fairly certain the widget background stuff is out of date. The background given is a single image, but the link to the Android docs take you to a page where the only available download is a whole folder of stuff. I don’t know how to use the single image provided, so I used the download instead. It contains a
res
folder with a bunch of folders starting withdrawable
in it. Their names were similar to those in my Android project’sres
directory, so I copied the icons generated by ADT when I set up my project into the downloadeddrawable
folders and then copied them wholesale into my project, replacing the ones that already existed. - If you use the downloaded resources from the Android docs like I did, you have to change the
android:background
attribute of theLinearLayout
tag ofmain.xml
to@drawable/appwidget_bg
. Without knowing too much about it, I believe the downloaded folder contains different resources for different screen sizes. This seems to be the relevant part of the Android docs.
- The
Step 5 has you edit
AndroidManifest.xml
. But it refers to a receiver namedWatchWidgetActivity
, which doesn’t exist because in step 1 you unchecked “Create Activity”. It should instead refer to your AppWidgetProvider subclass. So change.WatchWidgetActivity
toWatchWidget
.I think it’s a minor issue, but in step 4, Eclipse will complain about your setting the attribute
android:text
to a string literal. It should instead point to a string resource, because this aids greatly in localization. So to fix the warning, change the value ofandroid:text
to (say)@string/test_time
. Then open the fileres/values/strings.xml
and add a new String resource whose name should betest_time
. Its value can be whatever you want.In step 6,
android:updatePeriodMillis
is set to1000
. Having any widget refresh once every second is probably a terrible idea. I set mine to600000
.