5

Is there anyone knows how to refer to a defined string in a string definition, i.e.:

<string name="string_1">I am string 1</string>

and then,

<string name="string_2">I want to refer to @string/string_1</string>
<string name="string_3">@string/string_1 need to be referred here</string>

I've tried the sample codes, but it didn't work!

Ring Y
  • 61
  • 3
  • you can't reference another item within another item. – JoxTraex Mar 11 '13 at 08:08
  • Best is to do this in the code itself. – QVDev Mar 11 '13 at 08:11
  • Possible [duplicate1](http://stackoverflow.com/questions/4746058/reference-one-string-from-another-string-in-strings-xml), [duplicate2](http://stackoverflow.com/questions/3722374/android-how-to-inject-a-string-element-into-another-string-element-in-xml) – Rahul Mar 11 '13 at 08:14

2 Answers2

5

You could try using String.format():

<string name="string1">I am %1$s</string>
<string name="string2">String 1</string>

...
String string1 = String.format(resources.getString(R.string.string1, R.string.string2));
webmonkey
  • 1,083
  • 1
  • 15
  • 33
1

Yes you can do it at one condition as long as you reference the entire string

<string name="string_1">I am string 1</string>
<string name="string_2">I am string 2</string>

and then,

<string name="string_2">@string/string_1</string>
<string name="string_3">@string/string_2</string>

<string name="string_default">@string/string2</string>

but following is not supported

<string name="string_default">@string/string1 TEST</string>



You can also get your issue resolved by using String.format() (a good walk around)

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65