16

Is it possible to reference a string in strings.xml

Eg:

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

Where depending on requirements, i can switch the value of first_name to "Chrome", "Firefox" or "Opera".

Vinoth
  • 5,687
  • 11
  • 44
  • 56
  • possible duplicate of [Can one combine android resource strings into new strings?](http://stackoverflow.com/questions/3613722/can-one-combine-android-resource-strings-into-new-strings) – Graham Borland Apr 12 '12 at 10:34
  • possible duplicate of [Reference one string from another string in strings.xml?](http://stackoverflow.com/questions/4746058/reference-one-string-from-another-string-in-strings-xml) – sschuberth Jul 18 '13 at 15:36

3 Answers3

24

You can give the reference of string resource, but limitation are 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 content starts with "@" then Android considers this is a referenced string, see last case which gives an error because Android's tools take @ and the next string to it as the string's reference name, it will try to find a resource called "@string/first_name Chrome" which doesn't exist.

You can use String Format to dynamically assign sub-strings like <string name="application_name">%1$s browser</string>

to use

String text = String.format(res.getString(R.string.application_name), "Chrome");
Hassaan
  • 932
  • 8
  • 16
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
4

Yes, you can do so without having to add any Java/Kotlin code, using this small library that allows you to do so using XML only at buildtime. So for your case, you'd have to set up your strings like this:

<string name="application_name">${first_name} Browser</string>
<string name="first_name">Chrome</string>

And then after running the gradle plugin, you'll get this:

<!-- Auto generated during compilation -->
<string name="application_name">Chrome Browser</string>

This is the link to the library: https://github.com/LikeTheSalad/android-stem

Disclaimer: I'm the author of this library.

César Muñoz
  • 535
  • 1
  • 6
  • 10
2

The Strings in the strings.xml are fixed and cannot be changed at run time. You will have to define a string for each case, and do the switch in the code.

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);
}

You can however make the application select the correct string for different languages automatically. This can be done by placing string files in localized folders (values-en, values-fr, values-pl etc).

You can read more about localization at http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Rasmus Øvlesen
  • 510
  • 3
  • 8