Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1
Creating string resources

15

Creating string resources


Every project includes a default strings file named strings.xml.


Open res/values/strings.xml. The template has already added one string resource for you. Add the
three new strings that your layout requires.


Listing 1.3  Adding string resources (strings.xml)



GeoQuiz
Canberra is the capital of Australia.
True
False

(Depending on your version of Android Studio, you may have additional strings. Do not delete them.
Deleting them could cause cascading errors in other files.)


Now, whenever you refer to @string/false_button in any XML file in the GeoQuiz project, you will
get the literal string “False” at runtime.


If you had errors in activity_quiz.xml about the missing string resources, they should now be gone.
(If you still have errors, check both files for typos.)


Although the default strings file is named strings.xml, you can name a strings file anything you want.
You can also have multiple strings files in a project. As long as the file is located in res/values/, has a
resources root element, and contains child string elements, your strings will be found and used.


http://www.ebook3000.com

Free download pdf