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...').
1
  • This question is really a duplicate of this other question, and its accepted answer seems to be the best approach. Thanks to @Brian for pointing this out, Commented Jul 22, 2015 at 1:23

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 <string name="help4a_function">To use the function "@sting/a_function" you need to ...</string> I have to make 2 items "<string name="help4a_function_1">To use the function </string> and <string name="help4a_function_2"> you need to ...</string> and combine them with the contents of @sting/a_function. If I'm wrong please correct me!
You would have a string such as: <string name="help4a_function"> To use the function %1$s you need to ... </string> and assign the name of the function using String.format.
2

No, I don't think that is possible.

1 Comment

thank you for the quick answer. hopefully such a function will be added soon!
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;
}

Comments

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>

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.