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.xmlfile, which the tutorial doesn’t explain should be located inres/layoutin your project. Most of it works, but:- The
android:idattribute of theLinearLayouttag is wrong, which is unfortunate. The tutorial mentions in step 3 that Eclipse will complain aboutR.id.widget_textviewand 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:idattribute 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
resfolder with a bunch of folders starting withdrawablein it. Their names were similar to those in my Android project’sresdirectory, so I copied the icons generated by ADT when I set up my project into the downloadeddrawablefolders 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:backgroundattribute of theLinearLayouttag ofmain.xmlto@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.WatchWidgetActivitytoWatchWidget.I think it’s a minor issue, but in step 4, Eclipse will complain about your setting the attribute
android:textto 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:textto (say)@string/test_time. Then open the fileres/values/strings.xmland add a new String resource whose name should betest_time. Its value can be whatever you want.In step 6,
android:updatePeriodMillisis set to1000. Having any widget refresh once every second is probably a terrible idea. I set mine to600000.