Reference one string from another string in strings.xml?
Solution 1:
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>
Solution 2:
It is possible to reference one within another as long as your entire string consists of the reference name. For example this will work:
<string name="app_name">My App</string>
<string name="activity_title">@string/app_name</string>
<string name="message_title">@string/app_name</string>
It is even more useful for setting default values:
<string name="string1">String 1</string>
<string name="string2">String 2</string>
<string name="string3">String 3</string>
<string name="string_default">@string/string1</string>
Now you can use string_default
everywhere in your code and you can easily change the default at any time.
Solution 3:
I think you can't. But you can "format" a string as you like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="button_text">Add item</string>
<string name="message_text">You don't have any items yet! Add one by pressing the %1$s button.</string>
</resources>
In the code:
Resources res = getResources();
String text = String.format(res.getString(R.string.message_text),
res.getString(R.string.button_text));
Solution 4:
In Android you can't concatenate Strings inside xml
Following is not supported
<string name="string_default">@string/string1 TEST</string>
Check this link below to know how to achieve it
How to concatenate multiple strings in android XML?
Solution 5:
I created simple gradle plugin which allows you to refer one string from another. You can refer strings which are defined in another file, for example in different build variant or library. Cons of this approach - IDE refactor won't find such references.
Use {{string_name}}
syntax to refer a string:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="super">Super</string>
<string name="app_name">My {{super}} App</string>
<string name="app_description">Name of my application is: {{app_name}}</string>
</resources>
To integrate the plugin, just add next code into you app or library module level build.gradle
file
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.android-text-resolver:buildSrc:1.2.0"
}
}
apply plugin: "com.icesmith.androidtextresolver"
UPDATE: The library doesn't work with Android gradle plugin version 3.0 and above because the new version of the plugin uses aapt2 which packs resources into .flat binary format, so packed resources are unavailable for the library. As a temporary solution you can disable aapt2 by setting android.enableAapt2=false in your gradle.properties file.