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>
0

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Right, but that doesn't work for instance for strings being used in layout XML files, for instance.
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?
@StevenRoose That is possible as long as they're exactly the same. More details here: stackoverflow.com/a/6378421/357055

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.