Issue
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".
Solution
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");
Answered By - Mohammed Azharuddin Shaikh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.