Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Creating an Android App 349

Strings and other resources are given a name and a value, just like a vari-
able in Java. There are two string resources listed in the Resources ele-
ments pane: helloand app_name.


The names of resources follow three rules:


. They must be all lowercase.
. They must have no spaces.
. They must use only the underscore character (“_”) as punctuation.


Click a string in the Resources elements pane. Name and Value text fields
appear along with some guidance on how to edit strings (which also is
shown in Figure 24.3).


The app_namestring resource was something you chose when running the
New Android Project Wizard. The name should match what you gave it
earlier, but you can make changes at any time by editing this string.


The hellostring contains text todisplay on the app’s main (and only)
screen when it is run. Click the name of this string to bring it up for editing.


In the Value field, enter Saluton Mondo!.


Resources are stored in XMLfiles. The Resources editor is a simple XML
editor. You also can directly edit the XMLitself. Click the strings.xmltab
at the bottom of the editor to load this file for direct editing. (Refer to
Figure 24.3 where this tab is identified.)


Here’s what strings.xmllooks like at the moment:


Output▼


<?xmlversion=”1.0”encoding=”utf-8”?>



Saluton Mondo!
Saluton Mondo!

This editor allows everything in the XMLfile to be edited, even the markup
tags. The stringelement contains a nameattribute that identifies the name
of the resource. The value is enclosed within the tag as character data.


To go back to the Resources editor, click the Resources tab. Click the Save
button in the Eclipse toolbar to save your change to the file strings.xml.


With that modification, you’re almost ready to run the app.


CAUTION
Although you can edit XML
directly,don’t. There’s usually
no need to do it when creating
resourcesfor an Android app.
The exception is when the
Eclipse editor doesn’t support
something you want to do in
defining a resource. This isn’t
the case with strings,so it’s
better to stick to the Resources
editor. You’re more likely to
make errors editing the XML
files directly.
Free download pdf