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

AndroidAndroid LayoutData BindingAndroid Databinding

Android Problem Overview


Im using DataBinding Api for setting the views in android layouts. Here is my layout.

layout.xml

<?xml version="1.0" encoding="utf-8"?>
 <layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable name="user" type="testing.sampleapp.com.sampleapp.User"/>
  </data>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{ "Hello " + user.firstName}"/>
</LinearLayout>

I want the TextView to display Hello UserName. How to achieve this using the data binding api.

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

This is already answered by @GeorgeMount in comments to one of the solution. Which to me looks like the best solution so far here.

android:text="@{@string/location(user.city,user.state)}"

in your strings.xml

<string name="location">%1$s, %2$s</string>

Solution 3 - Android

Many ways to concat strings

1. Using string resource (Most preferable because of localization)

android:text= "@{@string/generic_name(user.name)}"

Just make string resource like this.

<string name="generic_name">Hello %s</string>
2. Hard coded concat
android:text="@{`Hello ` + user.name}"/>
3. Using String's concat method
android:text="@{user.firstName.concat(@string/space).concat(user.lastName)}"

Here space is an html entity which is placed inside strings.xml. Because XML does not accept Html entities or special characters directly. (Link Html Entities)

<string name="space">\u0020</string>
4. Using String.format()
android:text= "@{String.format(@string/hello, user.name)}"

you have to import String class in layout in this type.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <import type="String" />
    </data>
    <TextView
        android:text= "@{String.format(@string/hello, user.name)}"
        ... >
    </TextView>
</layout>
5. Another method
android:text="@{@string/generic_name(user.firstName,user.lastName)}"

In this case put a string resource in strings.xml

<string name="generic_name">%1$s, %2$s</string>

There can be many other ways, choose one you need.

Solution 4 - Android

Since xml supports single quotes for values of attribute, you can also do this :

android:text='@{"Hello "+user.firstName}'

Solution 5 - Android

There are two ways.

First Solution

concat with grave accent (`)

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

Second Solution

Declare Your string in strings.xml

like "Hello %1$s , (whatever you want to add then add here)".

amd use String.format(stringResource, upsatename);

Solution 6 - Android

In case of static string and other dynamic you can use this

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

In case of dynamic data you can use this.

android:text='@{user.firstName+" "+user.lastName}'

Solution 7 - Android

To do a concat in xml layout:

<data>

/*This is used for android view*/
<import type="android.view.View" />

/*This is used for android resources*/
<import type="com.myapp.R" />

/*This is app context*/
<variable
    name="context"
    type="android.content.Context" />

/*This is used for model data*/
<variable
    name="item"
    type="com.myapp.models.Chapter" />
</data>

android:text="@{item.serialNo.concat(@string/space).concat(item.title)}"

In strings.xml I have added code for blank space:

<string name="space">\u0020</string>

Solution 8 - Android

if you want to concat String resource with data from your model, you can do it in such way:

 android:text='@{@string/release_date+model.release_date}'

Solution 9 - Android

The simplest way I found to be is to replace ''(single) in place of ""(double), For Eg. You have two variables,

<variable name="a" type="String" />
<variable name="b" type="String" />

Now to concatenate,

android:text='a + " " + b}'

Solution 10 - Android

Probably late to the party: Below code will also work

android:text='@{@string/hello.concat(user.firstName)}'

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
QuestionSasank SunkavalliView Question on Stackoverflow
Solution 1 - AndroidRaviView Answer on Stackoverflow
Solution 2 - AndroidPrakashView Answer on Stackoverflow
Solution 3 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 4 - AndroidArJView Answer on Stackoverflow
Solution 5 - AndroidYogesh RathiView Answer on Stackoverflow
Solution 6 - Androidkishan vermaView Answer on Stackoverflow
Solution 7 - AndroidReady AndroidView Answer on Stackoverflow
Solution 8 - AndroidJackky777View Answer on Stackoverflow
Solution 9 - AndroidAzizView Answer on Stackoverflow
Solution 10 - AndroidmadroidView Answer on Stackoverflow