Concatenate Multiple Strings in Xml

Concatenate multiple strings in XML?

No I don't think you can concatenate.

<string name="aaa">aaa</string>
<string name="bbb">bbb @string/aaa</string>

Output - bbb @string/aaa

If you do,

<string name="aaa">aaa</string>
<string name="bbb">@string/aaa bbb</string> -> This won't work it
will give compilation error

Because here it will search for a String with reference @string/aaa bbb which does not exists.

Problem in your case was, you where using @strings/aaa which should be @string/aaa

I want to concat two strings for a TextView in android, Data Binding Api

concate it with grave accent (`)

android:text="@{`Hello ` + user.firstName}"/>

You can concat it in multiple ways, check it here concat-two-strings-in-textview-using-databinding

Concatenate Strings in the strings.xml file for Android

I am afraid that is not possible in strings.xml.

What you can do is create the final string programmatically using getString:

String outStr = getString(R.string.your_android_says) + 
" " + getString(R.string.hello);

Concatenation in XML

You must take reference to your TextView, then you can do something like this:

textView.setText(textView.getText().toString() + letter);

How can I concatenate static strings with XML string resources?

Sorry, no such syntax is supported by Android resource files.

Concatenate string and integer in string.xml

You can not concatenate strings or integer and strings in xml. You can do refer to another string or integer in one string or integer tag respectively in xml , but only one.
Like:

 <integer name="min_length">10</integer>

<integer name="min">@integer/min_length</integer>

You can refer integer inside integer and string inside string .

If you try to do what you mentioned above android studio , you will be thrown away with error.

how to concat two string resource in xml textview without binding

What you want is possible to be done statically.

But, a little search gave me this library LikeTheSalad

Extract of the README :

It allows you to do something like this:

<string name="app_name">world</string>
<string name="template_welcome_message">hello ${app_name}</string>

And then the library does this at build time:

<string name="welcome_message">hello world</string>


Related Topics



Leave a reply



Submit