Getting Started in Android Development Part 7 - The Strings Resource File

North Border Software




Home Tutorials Hints & Tips About

Android Apps and Training

Share on Facebook Share on Twitter

Getting Started in Android Development

Part 7 - The String Resource File

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:

android:text="@string/<string_name>"

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

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo