3

As the title says, my problem is that I don't know If/How I can use a String Resource in a String Resource in string.xml.

<string name="projectname">Include Test</string>
<string name="about">@string/projectname is to test if you can use a String Resource in a String Resource</string>

My question is if anyone knows if I can write

<string name="identifier">@string/other_identifier</string>

because Eclispe says

error: Error: No resource found that matches the given name (at 'other_identifier' with value '@string/other_identifier eingeben...').
hnzlmnn
  • 393
  • 2
  • 14
  • This question is really a duplicate of [this other question](http://stackoverflow.com/questions/3722374/android-how-to-inject-a-string-element-into-another-string-element-in-xml), and its accepted answer seems to be the best approach. Thanks to @Brian for pointing this out, – cybersam Jul 22 '15 at 01:23

5 Answers5

2

No, I don't think that is possible.

C0deAttack
  • 24,419
  • 18
  • 73
  • 81
2

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

Che Jami
  • 5,151
  • 2
  • 21
  • 18
  • But with String.format and combining the Stings at runtimeI have the problem that I want to use it in an help text. But if I want to write `To use the function "@sting/a_function" you need to ...` I have to make 2 items `"To use the function ` and ` you need to ...` and combine them with the contents of `@sting/a_function`. If I'm wrong please correct me! – hnzlmnn Sep 02 '11 at 14:10
  • You would have a string such as: ` To use the function %1$s you need to ... ` and assign the name of the function using `String.format`. – Che Jami Sep 03 '11 at 13:40
1

Edit: Actually this solution, using custom ENTITY entries is much nicer.


The following should work out of the box:

<string name="identifier">@string/other_identifier</string>

I just tried it with API Level 8 and it works fine. See also https://stackoverflow.com/a/6378421/786434

To be able to cross-reference strings within other words, you can create yourself a small helper method:

private static Pattern pattern = Pattern.compile("@string/(\\S+)");

public static String getString(Resources res, int stringID) {
String s = res.getString(stringID);

Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
    try {
    final Field f = R.string.class.getDeclaredField(matcher.group(1));
    final int id = f.getInt(null);
    final String replace = res.getString(id);
    s = s.replaceAll(matcher.group(), replace);

    } catch (SecurityException ignore) {
    } catch (NoSuchFieldException ignore) {
    } catch (IllegalArgumentException ignore) {
    } catch (IllegalAccessException ignore) {
    }
}

return s;
}
Community
  • 1
  • 1
Brian
  • 2,130
  • 22
  • 29
0

A nice way to insert a frequently used string (e.g. app name) in xml without using Java code here:

<?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>
Community
  • 1
  • 1
Beeing Jk
  • 3,796
  • 1
  • 18
  • 29
0

Yes you can :) Not natively though, but you can use this small library that allows you to resolve these placeholders at buildtime, so you won't have to add any Java/Kotlin code to make it work.

Based on your example, you'd have to set up your strings like this:

<string name="projectname">Include Test</string>
<string name="about">${projectname} is to test if you can use a String Resource in a String Resource</string>

And then the plugin will take care of creating the following:

<!-- Auto generated during compilation -->
<string name="about">Include Test is to test if you can use a String Resource in a String Resource</string>

More info here: https://github.com/LikeTheSalad/android-stem

Disclaimer: I'm the author of that library.

César Muñoz
  • 535
  • 1
  • 6
  • 10