If we are creating an Android application we need to write text on it. Labels, application information, fields, hints… lots of text and sentences are needed. How can we deal with this? We can do it in two ways: The wrong way and the Android way.
The wrong way is just putting the text “statically”, in the element we need. For example, in an xml TextView attribute:
android:Text=”Text Without using string.xml”
or in a TextView object in Android code:
textViewObject.setText(“Text without using string.xml”)
Well, we can think that this way of inserting text is not bad at all, but let’s analyze it a bit deeper.
Imagine that the person you are working with/for doesn’t like the text you have written, and wants to change all of it. Now, you have to check, line by line, file by file, in xml and in .java files all the text you have inserted and replace all of it. Interesting work, eh?… if we were monkeys, but we are programmers, so let’s do it in a right way.
Android gives us a very useful way to use text and sentences in our application. This is implemented by using the string.xml file inside the Resources folder. Here, all the strings are stored so they can be retrieved in the application. What happens if the boss-person want us to change all texts? Just go to this file and replace them. More easy, quick and effectively.
Let’s see how this string.xml file is composed.
<string name=“sentence2”>This is a Sentence</string>
For every String we want to put in our application, just add a line. In the “name” attribute, put the identifier of the string, something easy to remember and easily organizable, at the beginning of the application, maybe you have 10 or 20 strings…but if the app grows, you can easily have thousands of strings…so it’s important that you have the criteria to put in the name. Inside the tags, just put the string you want to use.
Now…how do you use this from the application?
We can use the strings we have created in the string.xml file, from two different places: From a layout defined in xml or from the Android code. Let’s go a bit further.
Strings in xml
This is the common way to use the strings, in our Resources, layout code. Let’s explain it with an easy example.
It’s that easy!! To reference a string from string.xml file we just need to use “@string/identifier_of_the_string” inside our “android:text” attribute. The next section delves into this use.
Strings in android code
As I wrote in the article How to create Android UI in Java Code, we learned that we can create a UI using Java code, so…to how do we add a text to a TextView object from the string.xml file?
The most important piece of code here is “this.getString(R.string.text1)”, the “this” statement is used when we are inside an Activity. If not, we need to use a “Context” object. Context is an “Interface to global information about an application environment” (via Google Android Page).
We will examine the “Context” aspect, in a following article, because Context is something we are going to use in all of our applications, therefore it’s important to know what it is and how to use it.
Have any questions about strings? Feel free to ask your questions in our comments section.
This post is part of the series: More Android Elements!!
More information about Android and its programing environment.