10

Somewhere I read how to use variables in XML document. They said it's very simple and I guess it was. I successfully used it that way in Android strings.xml file. I was using it that way the whole day until suddenly android stopped to parse it and stopped to treat it like a variable.

I used it in this way:

<resources>
<string name="some_string">string1</string>
<string name="another_string"> {$some_string} trolololo </string>
</resources>

and in java accessing it through: getApplicationContext().getString(R.strings.another_string);

getApplicationContext().getString(R.strings.another_string);

In the output I used to receive string like:

string1 trolololo

and now I receive only:

{$some_string} trolololo

Does anyone have any idea what is wrong? I know that Android's XML may differ than standard XML, but IT USED TO WORK. Awww... Thanks for any advice.

NioShobu
  • 775
  • 2
  • 10
  • 21
bpawlowski
  • 1,025
  • 2
  • 11
  • 22

4 Answers4

21

Assuming that you want to pass a string value as a parameter in the another_string then your string is not well formatted to receive that argument and if you try to use it your output will be {$some_string} trolololo.

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource.

<resources>
<string name="some_string">string1</string>
<string name="another_string">%1$s trolololo</string>
</resources>

Now your able to format the string with arguments from your application like this:

String arg = "It works!";
String testString = String.format(getResources().getString(R.string.another_string), arg);
Log.i("ARG", "another_string = " + testString);

Doing so the output string will be another_string = It works! trolololo.

Take a look at the Android Developers official documentation, here.

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • Thanks for the reply. I've already seen this method to format string(Earlier I was looking for solution on the Android Dev. documentation link you posted). And - Yes - That would solve my problem but I wanted to put all these strings into strings.xml file, prepare(concatenate) them there, and use these during runtime pre-prepared strings. The one thing I cannot understand is WHY it was working, and now it's not? – bpawlowski Jul 17 '12 at 05:19
7

This will solve your problem:

<resources>
    <string name="some_string">string1</string>
    <string name="another_string">@string/some_string trolololo</string>
</resources>

Now the output of the getApplicationContext().getString(R.strings.another_string) will be string1 trolololo.

Bogdan Kobylynskyi
  • 1,150
  • 1
  • 12
  • 34
  • The interesting thing is when I try this, I get an error, `No resource found that matches at the given name (at 'another_string' with value '@string/some_string trolololo').` – Jason L Jul 16 '12 at 20:20
  • [DevGuide](http://developer.android.com/guide/topics/resources/string-resource.html#String) says that in XML resource references '@string/string_name' – Bogdan Kobylynskyi Jul 16 '12 at 20:31
  • 1
    Yes, it's the XML file that's throwing the error. I didn't manage to edit that into the comment in time before it locked. – Jason L Jul 16 '12 at 20:32
  • @BogdanKobylinsky That is only useable in XML files other than the resource itself (such as views) – Tolga E Jul 16 '12 at 20:44
  • unfortunately this method with @string/some_string, doesn't. I saw the usage of strings in that way, but in the manifest file, but there it looks like that: – bpawlowski Jul 17 '12 at 05:23
1

Or, you can directly use getResources().getString(R.string.activity_title, arg).

For example

<resources>
   <string name="postfix_title">%s Gallery</string>
</resources>

and then simply,

String arg = "Colors";
String title = getResources().getString(R.string.postfix_title, arg);

This will result in title containing value Colors Gallery.

Sabeeh
  • 1,123
  • 9
  • 11
0

I'm not sure how the first initial thing you've done was working with the curly brackets but i've run into this issue before and couldn't find a solution..

Now what I do is calling those strings separately and concatenate them during runtime.

Tolga E
  • 12,188
  • 15
  • 49
  • 61
  • using that curly brackets I could reference to some string value within this local document, and obviously that allowed me to concatenate them. – bpawlowski Jul 17 '12 at 05:20