Android control string in string.xml

Is it possible to refer to a string in the strings.xml file

For instance:

<string name="application_name">@string/first_name Browser</string>
<string name="first_name">Chrome</string>

Depending on the requirements, I can switch the value of first_name to "Chrome", "Firefox" or "Opera".

+5
source share
2 answers

You can specify a link to a string resource, but the restriction is as follows

<string name="first_name">Chrome</string>
<string name="application_name">@string/first_name</string> // gives "Chrome"
<string name="application_name">Chrome @string/first_name</string> // gives "Chrome @string/first_name"
<string name="application_name">@string/first_name Chrome </string> // gives error

If the content starts with "@", then Android considers this to be a line with a link, see the last case that gives an error, because Android tools take @ and the next line as the name of the link, it will try to find a resource called "@string / first_name Chrome "which does not exist.

String Format , <string name="application_name">%1$s browser</string>

String text = String.format(res.getString(R.string.application_name), "Chrome");
+16

strings.xml . .

String name;

if (/* browser is Chrome*/) {
    name = getString(R.string.first_name_chrome);
} else if (/* browser is Firefox */) {
    name = getString(R.string.first_name_firefox);
}

. , (-en, values-fr, values-pl ..).

http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/

+2

All Articles