Reference One String from Another String in Strings.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>

Is it possible to reference another string in strings.xml?

I don't think it's possible.
What I usually do is the following:

<string name="string_one">My string</string>
<string name="string_two">Here it is: %s" </string>

and in the java code:

String.format(getString(R.string.string_two), getString(R.string.string_one));

I do this kind of thing for parametrize msgs like: "You have %d new mails".

Android reference string in string.xml

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

Can I use a String Resource in a String Resource in string.xml?

You are better off constructing these Strings at runtime. Combine different String resources together with the help of String.format.

Further details: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

How do you reference strings from strings.xml in code?

So I notice that your implementation doesnt necessarily have reference to a context so you will need to do something like this.

//method called to update textview strings.
public String getMyString(final int variable, final Context applicationContext){
switch(variable){
case 1:
return applicationContext.getResources().getString(R.string.something);
break;
case 2:
return applicationContext.getResources().getString(R.string.something2);
break;
case 3:
return applicationContext.getResources().getString(R.string.something3);
break;
}

}


Related Topics



Leave a reply



Submit