Reference one string from another string in strings.xml? Reference one string from another string in strings.xml? xml xml

Reference one string from another string in strings.xml?


A nice way to insert a frequently used string (e.g. app name) in xml without using Java code:source

    <?xml version="1.0" encoding="utf-8"?>    <!DOCTYPE resources [      <!ENTITY appname "MyAppName">      <!ENTITY author "MrGreen">    ]><resources>    <string name="app_name">&appname;</string>    <string name="description">The &appname; app was created by &author;</string></resources>

UPDATE:

You can even define your entity globaly e.g:

res/raw/entities.ent:

<!ENTITY appname "MyAppName"><!ENTITY author "MrGreen">

res/values/string.xml:

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE resources [    <!ENTITY % ents SYSTEM "./res/raw/entities.ent">    %ents;   ]><resources>    <string name="app_name">&appname;</string>    <string name="description">The &appname; app was created by &author;</string></resources>


It is possible to reference one within another as long as your entire string consists of the reference name. For example this will work:

<string name="app_name">My App</string><string name="activity_title">@string/app_name</string><string name="message_title">@string/app_name</string>

It is even more useful for setting default values:

<string name="string1">String 1</string><string name="string2">String 2</string><string name="string3">String 3</string><string name="string_default">@string/string1</string>

Now you can use string_default everywhere in your code and you can easily change the default at any time.


I think you can't. But you can "format" a string as you like:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="button_text">Add item</string>    <string name="message_text">You don't have any items yet! Add one by pressing the %1$s button.</string></resources>

In the code:

Resources res = getResources();String text = String.format(res.getString(R.string.message_text),                            res.getString(R.string.button_text));