Concatenate multiple strings in XML?

AndroidXmlString

Android Problem Overview


I go through this https://stackoverflow.com/questions/5764548/how-to-concatenate-multiple-strings-in-android-xml and in the end there are comments that

For clarity, Its works:

<string name="title">@string/app_name</string>.Andrzej Duś

I made my own example but it doesn't works. So does Andrzej wrong or I am doing something wrong in my code.

R.strings.bbb should contains "bbb aaa" but instead of "bbb aaa" it contains "bbb @strings/aaa"

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

Query:

Is it possible to do some concatenation only in xml, without source code changes?

Reason why I don't want to edit in code because I use this strings in xml/preferences.xml

For Example:

<ListPreference android:key="key_bbb" android:title="@string/bbb" ....

If you know what I mean, here there is no possibility to use something like this

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

Android Solutions


Solution 1 - Android

No, you can't concatenate strings in XML but you can define XML resources.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
  <!ENTITY appname "MyAppName">
  <!ENTITY author "MrGreen">
]>

<resources>
    <string name="app_name">&appname;</string>
    <string name="description">The &appname; app was created by &author;</string>
</resources>

The original answer was posted here.

Solution 2 - Android

In XML only this is not possible but using the java code you can use the String.format() method.

<string name="aaa">aaa</string>
<string name="bbb">bbb %1$s</string>

In java code

String format = res.getString(R.string.bbb);
String title = String.format(format, res.getString(R.string.aaa));

So title will be a full string after concatenation of two strings.

Solution 3 - Android

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

Solution 4 - Android

You can concatenate the resources in gradle.build:

    resValue "string", "TWITTER_CALLBACK", "twitter_callback_" + applicationId

Solution 5 - Android

Yes, you can do it if your XML files are using DataBinding as you can see in this link

Use it like this:

"@{@string/first_string+ @string/second_string}"

Solution 6 - Android

Kotlin version:

strings in the xml:

<string name="school_girl">Gogo Yubari</string>
<string name="assistant">Sofie Fatale</string>
<string name="boss">O-Ren Ishii</string>
<string name="list">%s, %s, %s</string>
 

and then in the code:

val killBillV1 = getString(R.string.list).format(
    Locale.US,
    getString(R.string.school_girl),
    getString(R.string.assistant),
    getString(R.string.boss)
)

Solution 7 - Android

Yes, you can do so without having to add any Java/Kotlin code, just XML, by using this library: https://github.com/LikeTheSalad/android-stem which does that at buildtime.

For your case, you'd have to set up your strings like this:

<string name="aaa">Some text</string>
<string name="bbb">bbb ${aaa}</string>

And then the library will create the following at buildtime:

<!-- Auto generated during compilation -->
<string name="bbb">bbb Some text</string>

Disclaimer: I'm the author of this library.

Solution 8 - Android

How about this technique

Use an array

<string name="app_link">https://myapp.com/</string>

<array name="update_dialog_msg">
	<item>Update can be downloaded manually by visiting this link:\n</item>
	<item>@string/app_link</item>
</array>

And then make a method in java class as below

public String getStringArray(int resStringArray)
{
	String str = "";
	
	String[] strArray = mContext.getResources().getStringArray(resStringArray);
	for (String s: strArray)
	{
		str += s;
	}
	
	return str;
}

Now call it from any java class as

mDialog.setMessage(getStringArray(R.array.update_dialog_msg);
// OR
textView.setText(getStringArray(R.array.update_dialog_msg);

Solution 9 - Android

Hi concat can do like this,

   android:text="@{userInfo.firstName.concat(@string/empty_str).concat(userInfo.lastName)}"
         

I guess its good sample for dynamic data. But if this sample containes other variation.Like User Name : Jon Doe

  android:text="@{String.format(@string/user_def,userInfo.firstName.concat(@string/empty_str).concat(userInfo.lastName))"
          

Thats it

Solution 10 - Android

try like this if you want add multiple string value

                        <TextView
                        ..........
                        android:text='@{String.format("%s %s","+91", userInfo.mobile)}'
                        .............. />

Solution 11 - Android

No, but is posible in xslt file with concat function:

<html>
      <body>
        <ul>
          <xsl:for-each select="stock">
            <xsl:if test="starts-with(@symbol, 'C')">
              <li>
                <xsl:value-of select="concat(@symbol,' - ', name)" />
              </li>
            </xsl:if>
          </xsl:for-each>
        </ul>
      </body>
    </html>

http://www.java2s.com/Tutorial/XML/0100__XSLT-stylesheet/concatfunction.htm

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLukapView Question on Stackoverflow
Solution 1 - AndroidSavelii ZagurskiiView Answer on Stackoverflow
Solution 2 - AndroidDharmendraView Answer on Stackoverflow
Solution 3 - AndroidLalit PoptaniView Answer on Stackoverflow
Solution 4 - AndroidBoyView Answer on Stackoverflow
Solution 5 - AndroidbatshevaView Answer on Stackoverflow
Solution 6 - AndroidnetporkView Answer on Stackoverflow
Solution 7 - AndroidCésar MuñozView Answer on Stackoverflow
Solution 8 - AndroidWaqar AslamView Answer on Stackoverflow
Solution 9 - AndroidNuman TurkeriView Answer on Stackoverflow
Solution 10 - AndroidSandeep PareekView Answer on Stackoverflow
Solution 11 - AndroidEjrr1085View Answer on Stackoverflow