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 String
s 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
Android: Allow Portrait and Landscape for Tablets, But Force Portrait on Phone
How to Play Videos in Android from Assets Folder or Raw Folder
How to Change Default Dialog Button Text Color in Android 5
Android P - 'Sqlite: No Such Table Error' After Copying Database from Assets
How to Import Existing Android Project into Eclipse
Android Lollipop Change Navigation Bar Color
Get Application Name from Package Name
Android Recyclerview VS Listview with Viewholder
Android, Canvas: How to Clear (Delete Contents Of) a Canvas (= Bitmaps), Living in a Surfaceview
Callback to a Fragment from a Dialogfragment
Error Opening Supportmapfragment for Second Time
How to Provide Shadow to Button
How to Start Service Using Alarm Manager in Android
How to Remove Application from App Listings on Android Developer Console
How to Add a Textview to a Linearlayout Dynamically in Android