Getting Started in Android Development
Part 7 -
So far we have hard coded the staring values directly into the activity_main.xml layout. This maybe fine if we just have one layout and want to support just one language in our app. However, what if the same string is used in many layouts and we wish to support multiple languages. The subject of supporting multiple language is covered here, so the focus of this tutorial is to look at how we use a string values file to centralize the actual string value and then use a reference in the layout file to access this string. Clearly, this is far more scalable and easier to support.
The first task is to locate the default strings.xml file. This is classed as resource and is found under the “values” folder with the parent folder “res”.
The default file should look similar to the one below. This has a reference to the default “hello world” and the app name.
Simply add additional string values for the views that have text in the activity_main.xml layout as below. Each has a name and the string value.
In the layout file, you need to replace the hard coded string value with a reference to the string name in the strings.xml file. This has the format below:
The “@string/<string_name>” indicates that the compiler needs to look for a string resource with the specified name.
After this tutorial, your activity_main.xml layout should look similar to the one below.
Download Download tutorial set 1