while (xpp.getEventType()! =XmlPullParser.END_DOCUMENT) {
if (xpp.getEventType()==XmlPullParser.START_TAG) {
if (xpp.getName().equals('word')) {
items.add(xpp.getAttributeValue(0));
}
}
xpp.next();
}
} catch (Throwable t) {
Toast
.makeText(this, 'Request failed: ' + t.toString(), 4000).show();
}
setListAdapter(new ArrayAdapter<String> (this,
android.R.layout.simple_list_item_1, items));
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
selection.setText(items.get (position).toString());
}
}
Now, inside our try...catch
block, we get our XmlPullParser
and loop until the end of the document. If the current event is START_TAG
and the name of the element is word xpp.getName().equals('word'))
, then we get the one-and-only attribute and pop that into our list of items for the selection widget. Since we’re in complete control over the XML file, it is safe enough to assume there is exactly one attribute. But, if you were not as comfortable that the XML is properly defined, you might consider checking the attribute count (getAttributeCount())
and the name of the attribute (getAttributeName()
) before blindly assuming the 0-index attribute is what you think it is.
As you can see in Figure 19-4, the result looks the same as before, albeit with a different name in the title bar.

Figure 19-4.
Miscellaneous Values
In the res/values/
directory, you can place one (or more) XML files describing simple resources: dimensions, colors, and arrays. We have already seen uses of dimensions and colors in previous examples, where they were passed as simple strings (e.g., '10px'
) as parameters to calls. You can, of course, set these up as Java static final objects and use their symbolic names… but this only works inside Java source, not in layout XML files. By putting these values in resource XML files, you can reference them from both Java and layouts, plus have them centrally located for easy editing.
Resource XML files have a root element of resources
; everything else is a child of that root.
Dimensions
Dimensions are used in several places in Android to describe distances, such as a widget’s padding. While this book usually uses pixels (e.g., 10px
for ten pixels), there are several different units of measurement available to you:
• in
and mm
for inches and millimeters, respectively, based on the actual size of the screen
• pt
for points, which in publishing terms is 1/72nd of an inch (again, based on the actual physical size of the screen)
• dp
and sp
for device-independent pixels and scale-independent pixels — one pixel equals one dp for a 160dpi resolution screen, with the ratio scaling based on the actual screen pixel density (scale-independent pixels also take into account the user’s preferred font size)
To encode a dimension as a resource, add a dimen
element, with a name attribute for your unique name for this resource, and a single child text element representing the value:
<resources>
<dimen name='thin'>10px</dimen>
<dimen name='fat'>1in</dimen>
</resources>
In a layout, you can reference dimensions as @dimen/...
, where the ellipsis is a placeholder for your unique name for the resource (e.g., thin and fat from the previous sample). In Java, you reference dimension resources by the unique name prefixed with R.dimen.
(e.g., Resources.getDimen(R.dimen.thin)
).
Colors
Colors in Android are hexadecimal RGB values, also optionally specifying an alpha channel.
You have your choice of single-character hex values or double-character hex values, leaving you with four styles:
• #RGB
• #ARGB
• #RRGGBB
• #AARRGGBB
These work similarly to their counterparts in Cascading Style Sheets (CSS).
You can, of course, put these RGB values as string literals in Java source or layout resources. If you wish to turn them into resources, though, all you need to do is add color elements to the resources file, with a name attribute for your unique name for this color, and a single text element containing the RGB value itself:
<resources>
<color name='yellow_orange'>#FFD555</color>
<color name='forest_green'>#005500</color>
<color name='burnt_umber'>#8A3324</color>
</resources>