81

Problem
I would like to be able to do something like that in Android XML code:

<string name="title">@string/app_name test</string>
<string name="title2">@string/app_name @string/version_name</string>

Regarding first line of code compiler shows this error

No resource found that matches the given name (at 'title' with value '@string/app_name test')

and regarding second line of code compiler shows this error

No resource found that matches the given name (at 'title2' with value '@string/app_name @string/version_name')

Question
Does anybody know how to concatenate multiple strings in Android XML?

Rationale
It is bad practice to duplicate variable values in many places of code (in this case XML).

Andrzej Duś
  • 1,338
  • 1
  • 10
  • 16

1 Answers1

60

I've tried to do a similar maneuver myself but I was told this is not doable in Android.

The interesting thing is that you get an error message that indicates that it indeed is possible but due to errors in resource matching it's not possible right now. (Are you sure you have defined the "app_name" and "version_name" strings before the "title" and "title2" strings?)

You can however do something like:

<string name="title">%1$s test</string>
<string name="title2">%1$s %2$s</string>

<string name="app_name">AppName</string>
<string name="version_name">1.2</string>

And then from Java code do something like:

Resources res = getResources();
String appName = res.getString(R.string.app_name);
String versionName = res.getString(R.string.version_name);

String title = res.getString(R.string.title, appName);
String title2 = res.getString(R.string.title2, appName, versionName);

More about formatting strings in Android.

Hope this helps.

Juuso Ohtonen
  • 8,826
  • 9
  • 65
  • 98
dbm
  • 10,376
  • 6
  • 44
  • 56
  • That is something. :-) Sadly you can't access fields formated this way from XML, e.g. `@string/title2` gives you _%1$s %2$s_. – Andrzej Duś Apr 23 '11 at 15:19
  • 3
    Yes, that is indeed the single largest drawback of this solution: it needs Java code to work. – dbm Apr 23 '11 at 15:32
  • 1
    What's more, this doesn't work too: `test @string/app_name` (but doesn't throw compile error, just stays unparsed). Seems that you are right: it's not doable what I'm asking for. – Andrzej Duś Apr 23 '11 at 15:34
  • It's quite odd though. You can reference a _dimension_ from another one. `@dimen/common_width` is fully valid if you previously have defined `10dp` – dbm Apr 23 '11 at 15:43
  • 9
    For clarity. That works too: `@string/app_name`. – Andrzej Duś Apr 23 '11 at 16:00
  • Aha! I've just learned something (thanks). – dbm Apr 23 '11 at 16:17
  • You might also want to look at this: http://stackoverflow.com/a/20035556/824950 – jclehner Jan 10 '14 at 11:05
  • do you think this will work: Only altavoz @string/only as i cant get it to work. but if i take out the altavoz and leave just the reference then it works. – j2emanue Jul 31 '15 at 20:33
  • It might be helpfull https://stackoverflow.com/questions/3656371/dynamic-string-using-string-xml/24903097#24903097 – varotariya vajsi Jul 29 '17 at 10:46