This class teaches you to
You should also read
Apps include resources that can be specific to a particular culture. For example, an app can include culture-specific strings that are translated to the language of the current locale. It's a good practice to keep culture-specific resources separated from the rest of your app. Android resolves language- and culture-specific resources based on the system locale setting. You can provide support for different locales by using the resources directory in your Android project.
You can specify resources tailored to the culture of the people who
use your app. You can provide any resource type that is
appropriate for the language and culture of your users. For example, the
following screenshot shows an app displaying string and drawable resources in
the device's default (en_US) locale and the Spanish
(es_ES) locale.
Figure 1. App using different resources depending on the current locale
If you created your project using the Android SDK
Tools (read Creating an
Android Project), the tools create a res/ directory in the top level of
the project. Within this res/ directory are subdirectories for various resource
types. There are also a few default files such as res/values/strings.xml, which holds
your string values.
Create Locale Directories and Resource Files
To add support for more locales, create additional directories inside
res/. Each directory's name should adhere to the following format:
<resource type>-b+<language code>[+<country code>]
For example, values-b+es/ contains string
resources for locales with the language code es. Similarly,
mipmap-b+es+ES/ contains icons for locales with the es
language code and the ES country code.
Android loads the appropriate resources according to the locale settings of the
device at runtime. For more information, see
Providing Alternative Resources.
After you’ve decided on the locales to support, create the resource subdirectories and files. For example:
MyProject/
res/
values/
strings.xml
values-b+es/
strings.xml
mipmap/
country_flag.png
mipmap-b+es+ES/
country_flag.png
For example, the following are some different resource files for different languages:
English strings (default locale), /values/strings.xml:
<resources>
<string name="hello_world">Hello World!</string>
</resources>
Spanish strings (es locale), /values-es/strings.xml:
<resources>
<string name="hello_world">¡Hola Mundo!</string>
</resources>
United States' flag icon (default locale),
/mipmap/country_flag.png:
Figure 2. Icon used for the default (en_US) locale
Spain's flag icon (es_ES locale),
/mipmap-b+es+ES/country_flag.png:
Figure 3. Icon used for the es_ES locale
Note: You can use the locale qualifier (or any configuration qualifier) on any resource type, such as if you want to provide localized versions of your bitmap drawable. For more information, see Localization.
Use the Resources in your App
You can reference the resources in your source code and other XML files using
each resource's name attribute.
In your source code, you can refer to a resource using the syntax
R.<resource type>.<resource name>. There are a variety
of methods that accept a resource this way.
For example:
// Get a string resource from your app'sResourcesString hello =getResources().getString(R.string.hello_world); // Or supply a string resource to a method that requires a string TextView textView = new TextView(this); textView.setText(R.string.hello_world);
In other XML files, you can refer to a resource with the syntax
@<resource type>/<resource name> whenever the XML attribute accepts a compatible value.
For example:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/country_flag" />