Sunday, 11 September 2011

Obtain values of Strings.xml


The file Strings.xml in the folder values is an excellent place to store and unify all the texts that appears in the program, no matter if they are the name of the buttons or a long alert message text.

The structure of it is very simple.

<string name="NameOfTheString">Text to show</string>

For example:
<string name="startButton">Start</string>

In case when you need to refer it, you can follow:

From Java file:
The simplest option is use directly
R.string.NameOfTheString

In the example, it could be:
R.string.startButton

Notice R is a self generated file. All the changes made in the Strings.xml should appears there. If not, press on Project -> clean ... and click on Ok. The this file will be generated again.

From Layout file(XML):
To refer any values you can use:
@string/NameOfTheString

In this example, it could be:
@string/startButton

2 comments:

  1. I am using Text to show
    ,its showing an error...xml5487cne bit not found ? what it is.




    Cv Template

    ReplyDelete
  2. @syeds: Could you specify a bit better your problem?

    ReplyDelete