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

2

3 Answers 3

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 = res.getString(R.string.application_name,"Chrome");
Sign up to request clarification or add additional context in comments.

1 Comment

Can format the string using - res.getString(R.string.application_name,"Chrome")
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.

Comments

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/

1 Comment

Is it an error or is it correct for your usage that you use "R.strings" instead of "R.string"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.