One of the best practices of Android application development is to write all strings in a single configuration file, which is strings.xml. This puzzles many developers at first. Although nothing stops you from writing text in your layout controls directly and this would not stop the application from running, the mostly widely used Android IDEs, such as Eclipse, will show you warnings if you do.
The suggested practice is to write a key-value pair in the strings.xml file where the key is what you refer to in your layout control such as TextView or Button as "@strings/key" and the value is the actual text that you want the view to display.
To many people, this practice would seem inefficient, as it obviously takes more time to write the configuration entry than to just write text in one place, especially when the key has to be longer than the actual value that it represents. After all, this methodology is not generally used in development of different types of software. However, there are some very valid reasons why properly using strings.xml in Android application is a good idea. Here are the 3 most fundamental ones.
1 Labels shared by many layouts or controls
Shared strings can be considered to be the same as static variables used by several classes in any software application. So, if your app uses the same title on several layouts it would be very easy to update it everywhere. As long as the key of the string value remains the same, all you have to do is to change the value in strings.xml file.
Using strings.xml file allows you to translate your app into different languages without rewriting every single layout or re-publishing the whole application. All you have to do is to create localised version of "values" folder (e.g. values-fr for French, values-jp for Japanese) and place its own version of string.xml file there. Users will see the text in the same app in different languages depending on the device settings.
3 Risks with tablet-based development
Many developers chose to write their app on Android devices directly by using specialised IDEs such as AIDE. There are many pros and cons of this approach; however one of the annoying things is that it is fairly easy to accidentally delete something without any options to restore it.
Although strings.xml file will not eliminate the risk of accidentally deleting the files, it will mitigate it somewhat. If deletion has occurred, you would still need to write the code from scratch. However, you will not have to write all the text values within it. Same applies to using shared colour codes and dimensions.
If you feel that there are more important reasons for using strings.xml, please write to us on firstname.lastname@example.org and we will be happy to add them.
Written by Mobile Tech Tracker team
Posted on 29 Aug 2015