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 in
res/layoutin your project. Most of it works, but:
android:idattribute of the
LinearLayouttag is wrong, which is unfortunate. The tutorial mentions in step 3 that Eclipse will complain about
R.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 the
android:idattribute should actually be
- 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 with
drawablein it. Their names were similar to those in my Android project’s
resdirectory, so I copied the icons generated by ADT when I set up my project into the downloaded
drawablefolders 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 the
@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.
Step 5 has you edit
AndroidManifest.xml. But it refers to a receiver named
WatchWidgetActivity, which doesn’t exist because in step 1 you unchecked “Create Activity”. It should instead refer to your AppWidgetProvider subclass. So change
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 of
@string/test_time. Then open the file
res/values/strings.xmland add a new String resource whose name should be
test_time. Its value can be whatever you want.
In step 6,
android:updatePeriodMillisis set to
1000. Having any widget refresh once every second is probably a terrible idea. I set mine to