Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to refer to strings in a string definition [closed]

Tags:

string

android

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!

like image 509
Ring Y Avatar asked Mar 11 '13 08:03

Ring Y


2 Answers

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));
like image 79
webmonkey Avatar answered Sep 27 '22 19:09

webmonkey


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)

like image 28
Festus Tamakloe Avatar answered Sep 27 '22 17:09

Festus Tamakloe