Professional Documents
Culture Documents
ANDROID SDK
In this tutorial, you will learn how to create and use alternative resource files with an
Android application called One Minute Phrasebook. This application displays certain
text and graphics based upon the Android system language/locale setting.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Before You Begin
The authors are assuming the reader has some basic knowledge of Android and
has all of the necessary tools, such as Eclipse and the Android SDK, installed and
working. The examples given here are engineered to show how Android project
resources can be localized. Specifically, this tutorial will show you how to include
resources for different languages and locales.
To do this within Eclipse, choose File->New Android Project to launch the Android
project wizard.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Choose an appropriate Build Target, such as Android 2.1.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 2: Configure the Application for Debugging
You will be debugging this application, so you need to set the Debuggable attribute
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
in the Android Manifest file to true.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 3: Create a Debug Configuration
You will want to be able to easily launch the application in the Android emulator and
on the device from Eclipse, so you need to create a Debug Configuration.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 4: Create Default String Resources
The One Minute Phrasebook displays the locale as well as several strings (Hello,
Goodbye, Please, Thank You and Help!) on the screen. Therefore, you need
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
to add a bunch of string resources to the /res/values/strings.xml file. Specifically,
add one string for each phrase, as well as a label for each phrase. Also, add strings
for the Flag (label), Map (label), and CIA World Factbook link (more on this in a
moment).
For default strings, we use one of the most common languages understood around
the worldEnglish. (Certainly, you could choose another language for your default, if
you like.)
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
16
17 <string name="thankyou_label">Thank Someone: </
string>
18 <string name="thankyou">Thank You! </string>
19
20 <string name="help_label">Call for Aid: </string>
21 <string name="help">Help! </string>
22
23 <string name="flag_label">Flag: </string>
24 <string name="map_label">Map: </string>
25
26 <string name="cia_factbook_label"
>CIA World Factbook URL: </
string
27 <string name="cia_factbook_url"
>"https://www.cia.gov/library/publications/the-wo
28 </resources>
These are the default string resources that are used by this application. Many
applications use only default resources.
For the default cause, we will say the locale is World-wide, and display a graphic
with a bunch of world maps, and a map of the planet.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 6: Design the One Minute Phrasebook User
Interface
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Next, turn your attention to designing the user interface of the application. The One
Minute Phrasebook is a very simple application with just one screen. The screen
displays some useful phrases, a flag and a map for the country, and a link to the
CIA World Factbook website for the appropriate country.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Note: For this application, we use an Android Virtual Device (AVD) profile to closely
match that of the Nexus One, with its 800x480 screen. Feel free to create an AVD
to match whichever Android device you have on hand to test with.
In this case, we want nice, formatted columns, so consider using the TableLayout
control to format the TextView and ImageView controls containing the phrase, flag,
map and link to the CIA World Factbook.
The resulting main.xml layout file should look something like this:
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
001 <?xml version="1.0" encoding="utf-8"?>
002 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
003 android:orientation="vertical" android:layout_width ="fill_parent"
004 android:layout_height="fill_parent">
005 <ScrollView android:id="@+id/ScrollView01"
006 android:layout_width ="wrap_content" android:layout_height="wrap_content"
007 android:layout_gravity ="center">
008 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
009 android:orientation ="vertical" android:layout_width="fill_parent"
010 android:layout_height ="fill_parent">
011 <TableLayout android:id="@+id/TableLayout01"
012 android:layout_margin="@dimen/phrase_margin"android:layout_height
013 android:layout_width="fill_parent" android:stretchColumns
014 <TableRow android:id="@+id/TableRow01"
015 android:layout_height ="wrap_content" android:layout_width
016 <TextView android:id="@+id/TextView_LocaleLabel"
017 android:layout_width ="wrap_content" android:layout_height
018 android:text="@string/locale_label"android:textSize
019 android:textColor="@color/locale_color" ></TextView
020 <TextView android:id="@+id/TextView_Locale"
021 android:layout_height ="wrap_content" android:text
022 android:textSize="@dimen/phrase_size"android:textColor
023 android:layout_width ="wrap_content" android:layout_gravity
024 </TableRow>
025 <TableRow android:id="@+id/TableRow02"
026 android:layout_width ="wrap_content" android:layout_height
027 <TextView android:id="@+id/TextView_HelloLabel"
028 android:layout_width ="wrap_content" android:layout_height
029 android:text="@string/hello_label"android:textSize
030 android:textColor="@color/label_color" ></TextView
031 <TextView android:id="@+id/TextView_Hello"
032 android:layout_height ="wrap_content" android:text
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
032 android:layout_height="wrap_content" android:text
033 android:textSize="@dimen/phrase_size"android:layout_width
034 android:textColor="@color/phrase_color"android:layout_grav
035 </TableRow>
036 <TableRow android:id="@+id/TableRow03"
037 android:layout_width="wrap_content" android:layout_height
038 <TextView android:id="@+id/TextView_GoodbyeLabel"
039 android:layout_width="wrap_content" android:layout_height
040 android:text="@string/goodbye_label"android:textSize
041 android:textColor="@color/label_color"></TextView
042 <TextView android:id="@+id/TextView_Goodbye"
043 android:layout_width="wrap_content" android:layout_height
044 android:text="@string/goodbye" android:textSize
045 android:textColor="@color/phrase_color"android:layout_grav
046 </TableRow>
047 <TableRow android:id="@+id/TableRow04"
048 android:layout_width="wrap_content" android:layout_height
049 <TextView android:id="@+id/TextView_PleaseLabel"
050 android:layout_width="wrap_content" android:layout_height
051 android:text="@string/please_label"android:textSize
052 android:textColor="@color/label_color"></TextView
053 <TextView android:id="@+id/TextView_Please"
054 android:layout_width="wrap_content" android:layout_height
055 android:text="@string/please" android:textSize
056 android:textColor="@color/phrase_color"android:layout_grav
057 </TableRow>
058 <TableRow android:id="@+id/TableRow05"
059 android:layout_width="wrap_content" android:layout_height
060 <TextView android:id="@+id/TextView_ThankyouLabel"
061 android:layout_width="wrap_content" android:layout_height
062 android:text="@string/thankyou_label"android:textSize
063 android:textColor="@color/label_color"></TextView
064 <TextView android:id="@+id/TextView_Thankyou"
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
064 <TextView android:id="@+id/TextView_Thankyou"
065 android:layout_width="wrap_content" android:layout_height
066 android:text="@string/thankyou" android:textSize
067 android:textColor="@color/phrase_color"android:layout_grav
068 </TableRow>
069 <TableRow android:id="@+id/TableRow06"
070 android:layout_width="wrap_content" android:layout_height
071 <TextView android:id="@+id/TextView_HelpLabel"
072 android:layout_width="wrap_content" android:layout_height
073 android:text="@string/help_label"android:textSize
074 android:textColor="@color/label_color"></TextView
075 <TextView android:id="@+id/TextView_help"
076 android:layout_width="wrap_content" android:layout_height
077 android:text="@string/help" android:textSize
078 android:textColor="@color/phrase_color"android:layout_grav
079 </TableRow>
080 </TableLayout>
081 <TableLayout android:id="@+id/TableLayout02"
082 android:layout_margin="@dimen/phrase_margin"android:layout_height
083 android:layout_width="fill_parent">
084 <TableRow android:id="@+id/TableRow07"
085 android:layout_width="wrap_content" android:layout_height
086 <TextView android:id="@+id/TextView_FlagLabel"
087 android:layout_width="wrap_content" android:layout_height
088 android:text="@string/flag_label"android:textSize
089 android:textColor="@color/label_color"></TextView
090 </TableRow>
091 <TableRow android:id="@+id/TableRow08"
092 android:layout_width="wrap_content" android:layout_height
093 <ImageView android:id="@+id/ImageView01"
094 android:layout_width="wrap_content" android:layout_height
095 android:src="@drawable/flag" android:adjustViewBounds
096 android:saveEnabled="true"></ImageView>
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
096 android:saveEnabled="true"></ImageView>
097 </TableRow>
098 <TableRow android:id="@+id/TableRow09"
099 android:layout_width="wrap_content" android:layout_height
100 <TextView android:id="@+id/TextView_MapLabel"
101 android:layout_width="wrap_content" android:layout_height
102 android:text="@string/map_label"android:textSize
103 android:textColor="@color/label_color"></TextView
104 </TableRow>
105 <TableRow android:id="@+id/TableRow10"
106 android:layout_width="wrap_content" android:layout_height
107 <ImageView android:id="@+id/ImageViewMap"
108 android:layout_width="wrap_content" android:layout_height
109 android:src="@drawable/map"></ImageView>
110 </TableRow>
111 <TableRow android:id="@+id/TableRow11"
112 android:layout_width="wrap_content" android:layout_height
113 <TextView android:id="@+id/TextView_CIAFactbookLabel"
114 android:layout_width="wrap_content" android:layout_height
115 android:textSize="@dimen/phrase_size"android:textColor
116 android:text="@string/cia_factbook_label"></
117 </TableRow>
118 <TableRow android:id="@+id/TableRow12"
119 android:layout_width="wrap_content" android:layout_height
120 <TextView android:id="@+id/TextView_help"
121 android:layout_width="wrap_content" android:layout_height
122 android:textSize="@dimen/phrase_size"android:textColor
123 android:layout_gravity="right" android:text
124 android:autoLink="web"></TextView>
125 </TableRow>
126 </TableLayout>
127 </LinearLayout>
128 </ScrollView>
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
128 </ScrollView>
129 </LinearLayout>
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 9: Changing the Device or Emulator Locale
Now its time to explore the language settings of your emulator or device. To view
and change the language/locale settings from the Home screen, click the Menu
button and choose Settings->Language and Keyboard->Select Locale.
Try changing the locale to a foreign language you are familiar with.
Warning: Keep in mind that if you change the language and leave the Settings, you
will need to be able to navigate back to the settings screen in that language.
Memorizing, or writing down, the keystrokes to do this can simplify doing so.
Note how all the menus change to that language. Run the One Minute Phrasebook
application in this locale. Note how the application does not appear any different
than before you changed the localeit still appears in English.
Now, change back to your favored locale. Mine is English (United States).
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Note: The emulator has all locale options available in a given version of the Android
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
SDK. However, specific devices may have a limited set of locales to choose from.
When alternate resources are included with an application as part of their package,
the Android operating system always attempts to load the most specific resources
available.
Alternate resources can be created for many different criteria, including, but not
limited to, languages and regions, screen characteristics and device input methods.
Some common alternative resources are those for languages, locales and screen
orientation (layouts especially). Criteria are organized hierarchically and each type
of criteria can be used as a suffix on the appropriate project resource directory
name.
Alternate resources must use the same names as the default resources. Android
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
resource resolution is best shown by example. In this tutorial, we focus only on
creating alternate resources for specific languages and regions.
You can add language resources to any project by creating specially named project
directories. Simply take the existing project directory name (such as /res/values)
and tack on a dash followed by the two letter language code, as defined in ISO 639-
2. So, for example, English is en, French is fr, Spanish is es, German is de, etc.
You can add a single set of French translated strings in the /res/values-fr/strings.xml
file. These strings will be loaded whenever a French-speaking locale setting is
enabled. Any other locale settings will continue to use the default strings.
Each of the alternate resource strings must have the same name as the default
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
resource used (stored in /res/values/strings.xml). The Android operating system will
choose the most specific resource available at runtime. You do not need to provide
translations for all strings, just the ones you want altered when a French-speaking
locale setting is chosen.
Note that you may need to quote resource strings if they contain single quotes
themselves (such as S'il vous plat).
To create resources specific to a region, you must use its language (in this case
fr), followed by a dash, followed by its region code in the form rXX where XX is
the region code for the country as defined by ISO 3166-1-alpha-2 code.
For example, the language and region codes for the French-speaking countries
supported in Android would be:
Belgium: fr-rBE
Canada: fr-rCA
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
France: fr-rFR
Switzerland: fr-rCH
So, for example, you can create a strings.xml file in the /res/values-fr-rBE project
directory for string values specifically for the French (Belgium) locale setting.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
So to add flag and map drawable resources for each country (Belguim, Canada,
France, and Switzerland), you need to create four new directories under the project:
Note that the drawable file names must match the default drawable file names
(those stored in /res/drawable) exactly.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 14: Re-running with a French-speaking
Locale
In your emulator or on your device, change the Locale to one of the French options
(Franais (France), Franais (Canada), Franais (Belguim), Franais (Switzerland).)
Note: For those not versed in French, you can return to the locale settings menu
from the Home screen by choosing: Menu->Paramtres->Langue et clavier-
>Langue et rgion.
Re-run the One Minute Phrasebook application and view the results. Note how the
appropriate French strings are loaded for all French languages. Also note how the
specific region dictates which country information is displayed within the application.
The figure shows what happens when you choose Franais (France).
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Step 15: Where to Go From Here
Internationalization forces design choices on the development team. For example,
will you build one big project for all languages, or will you break the applications up
by region? For some projects with light internationalization, you may be able to get
away with one project with all internationalized resources. For deep
internationalization, you may need to reorganize projects so that no application
becomes too large or cumbersome for the user.
You may have noted that in all this internationalization work, we never once touched
the java source files of the project. This is important to note, because it means that
the work of internationalizing an application may fall not to a developer who knows
how to use Eclipse and the Android tools well, but to someone else who will need
some training on how Android internationalization works, how resources can be
layered, and the drawbacks of over-internationalizing (resulting in very large
package files with many graphics and such). On the plus side, this leaves
developers free to do what they do best: developing code.
Lastly, you likely noted that the Android internationalization structure isnt perfect
especially for countries with multiple official (and unofficial) languages. It makes little
sense to have to include the same graphics (flag and map) in both an en-rCA and
fr-rCA directories for English and French speaking Canadians. However, you can
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
work around these limitations by programmatically determining the locale and
loading the appropriate graphic files yourself. That, however, is for another tutorial.
You can find a more complete explanation of Android resource resolution in the
Android SDK documentation here.
Conclusion
In this tutorial, you learned how to create and use alternative resource files with an
Android application called One Minute Phrasebook. This application displays certain
strings and graphics based upon the Android system language/locale setting. You
learned about how the Android operating system resolves resources and chooses
the most appropriate resource available at runtime. You also learned how to
organize resources efficiently in order to provide your worldwide users with the best
possible application experience.
We hope you enjoyed this tutorial and look forward to your feedback!
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Advertisement
Advertisement
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Categories:
Translations Available:
Download Attachment
Related Tutorials
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Code
Jobs
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
11 Comments Mobiletuts+
Sort by Best
One question I have is how would you implement a language that isn't roman alphabet based, ie Janpanes
(Both Mandarin and Cantonese), etc?
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
(Both Mandarin and Cantonese), etc?
Reply Share
I am aware of Creating a new values directory for the language with the suffix of the language code. For g
values-de or french: values-fr then copy our string.xml into that and translate each entry. And this works b
Phone Localization settings
I wanted to know if we can bypass the phone setting and and make the user select his required language
app?
My requirement is, i want to give a language selection option inside my app, and make the user select the
he wants for the app.. how to dynamically switch between the string.xml (for different languages) ???
thanks in advance
Reply Share
maybe it's possible not to place actual files in directories but links to em?
Reply Share
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Facebook App
Events
Learn How People Engage
with Your App and Measure
Performance.
Advertisement
Email Address
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
Help and Support Email Address
FAQ
Subscribe
Terms of Use
Contact Support Privacy Policy
About Tuts+
Advertise
Teach at Tuts+
Build anything from social networks to file upload systems. Build faster
with pre-coded PHP scripts.
Browse PHP on CodeCanyon
2014 Envato Pty Ltd. Trademarks and brands are the property of their respective
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com
owners.
open in browser PRO version Are you a developer? Try out the HTML to PDF API pdfcrowd.com