Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Default resources


Default resources


The configuration qualifier for English is -en. In a fit of localization, you might think to rename your
existing values directory to values-en/. This is not a good idea, but pretend for a moment you did
just that: Your hypothetical update means your app now has an English strings.xml in values-en and
a Spanish strings.xml in values-es.


Your newly updated app will build just fine. It will also run just fine on devices with the language set
to Spanish or English. But what happens if the user’s device language is set to Italian? Bad things.
Very bad things. At runtime Android will not find string resources that match the current configuration.
The severity of the resulting bad behavior depends on where the resource ID for a given string is being
referenced.


If an unmatched string resource is referenced in your XML layout file, the app will display the resource
ID of the resource (rather than a meaningful string you have defined). To see this behavior in action,
comment out the crime_title_label entry from values/strings.xml. (You can easily comment out
a single line by clicking on the line and then pressing Command+/ [Ctrl+/].)


Listing 18.2  Commenting out English crime_title_label
(res/values/strings.xml)



IntentoCriminal
Introduzca un título para el crimen.

...

(Recall that crime_title_label is referenced in fragment_crime.xml:)


<TextView
style="?android:listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_title_label" />

Free download pdf