Working with Strings in Android Development

Written by:  • Edited by: Simon Hill
Updated Jul 1, 2010
• Related Guides: Android | Google Android Programming

In this article we will learn how to use the strings.xml file in our Android programming environment.

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.

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="text1">Text</string>

<string name="sentence2">This is a Sentence</string>

...

</resources>

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 <string> 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.

<TextView

android:id="@+android:id/textView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=”@string/text1”

/>

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?

textViewObject.setText(this.getString(R.string.text1));

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).

Want more?

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.


Comments

Showing all 10 comments
 
ravi Jul 25, 2011 11:49 AM
Android string.xml
I am Android beginer. I not able to access string.xml to java code in Android. Following is code

<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item></string-array>


Resources res= getResources();
String[] planets=res.getStringArray(R.array.planets);
ravi Jul 25, 2011 4:11 AM
Android String.xml
I new to Android. I am not able to access stringArray in string.xml to javacode.Following is array and java code i used

<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item></string-array>



Resources res= getResources();
String[] planets=res.getStringArray(R.array.planets);
Gatz Apr 19, 2011 5:04 AM
Strings
Is there a way to save a string to the strings.xml from the application.

We have two strings a and b. We make a+b and save it to the strings.xml as ab. Can something like this be done? I'm new to android programin, so excuse my noob questions :)
Robin Dec 24, 2010 12:28 PM
Arrays
How do I write multidimensional string arrays in the string.xml file? I want data somewhat set up like this:

myArray[0] = ("How old is earth?", "4.5 billion years", "www.ageofearth.com", "earth.jpg");

myArray[1] = ("Number of planets in our solar system?", "Eight", "www.nineplanets.org", "eightplanets.jpg")

Basically a question, answer, external link, and perhaps a graphic element. Thanks!
Rizwan Nov 13, 2010 11:25 PM
Strings.xml change text from online content
Hi, im a beginner to android and stuck at this problem. In my strings.xml file i have a field "about" with some text about the application.

I want this text to be changed (value calculated in another activity and is stored in a string variable). how can i do that?

I just want whole layout to be same except this field value which will be calculated at run time, and i do not want the value from strings.xml to be read.

Please help
Jbeerdev Oct 27, 2010 2:58 AM
RE: Working with Strings in Android Development
Hi Ganesh

As far as I know, can be more string.xml files, but for differente languages, not devices.

If you want to place different strings for differente devices you should do by code.
Ganesh Oct 26, 2010 6:43 AM
How to give conditions in android XMLs
Hi All.,

My appication is used across different devices. For all device (SPH-
D800,MOTO-Droid. etc) i force to make use of one string.xml and one
android manefiest file for my application.

Can any please tell me how can i make the conditions inside the all
xml file.

Thanks in advance ! !

Thanks.,
Ganesh Kumar R.
Overv Oct 23, 2010 3:29 PM
RE: Working with Strings in Android Development
Michael, use &lt; instead of < to use a < in a string and use &gt; for >.
Michael Zak Sep 16, 2010 5:14 AM
Strings.xml: "<" within the string value
Hi,

If it possible to use character "<" within string value?

Thanks in advance
Michael
Paul Apr 18, 2010 11:45 AM
Strings.xml
What if your strings.xml had two strings and you wanted to reference one from the other:

<string name="app_name">blah</string>
<string name="main_page">app_name + more blah</string> ?
 
blog comments powered by Disqus
Email to a friend