12

Is it possible to reference one string in strings.xml inside another string that contains other text?

Something that is allowed in XML, and would achieve this effect:

<string name="string_one">My string</string>
<string name="string_two">Here it is: android:id="@string/string_one"</string>
ATL
  • 444
  • 6
  • 10
ChaimKut
  • 2,759
  • 3
  • 38
  • 64

2 Answers2

14

You can create your own XML entities for the strings you want to use in other strings, and use them like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
    <!ENTITY app_name "My App">
]>
<resources>
    <string name="app_name">&app_name;</string>
    <string name="welcome_message">Welcome to &app_name;</string>
</resources>
Matt
  • 74,352
  • 26
  • 153
  • 180
ATL
  • 444
  • 6
  • 10
12

I don't think it's possible. What I usually do is the following:

<string name="string_one">My string</string>
<string name="string_two">Here it is: %s" </string>

and in the java code:

String.format(getString(R.string.string_two), getString(R.string.string_one));

I do this kind of thing for parametrize msgs like: "You have %d new mails".

Macarse
  • 91,829
  • 44
  • 175
  • 230
  • Right, but that doesn't work for instance for strings being used in layout XML files, for instance. – ChaimKut May 03 '10 at 12:02
  • It is not possible to do this statically? F.e. if you want to have 2 string with different names but always being the same you could link the second to the first so you only have to change one. That would be possible with a static link between them both. So that's not possible? – Steven Roose Sep 11 '12 at 18:03
  • 1
    @StevenRoose That is possible as long as they're exactly the same. More details here: http://stackoverflow.com/a/6378421/357055 – acj Oct 24 '12 at 15:52