125

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.

Ravi
  • 33,034
  • 19
  • 115
  • 176
Sasank Sunkavalli
  • 3,514
  • 4
  • 27
  • 53

11 Answers11

308

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

Ravi
  • 33,034
  • 19
  • 115
  • 176
88

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>
iCantC
  • 2,262
  • 1
  • 13
  • 28
Prakash
  • 7,325
  • 4
  • 46
  • 44
63

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.

Khemraj Sharma
  • 52,662
  • 21
  • 188
  • 195
  • Option 1 worked for me. Option 4 didn't work. If you have a narrow adapter, I recommend Option 1. – rminaj Jul 12 '19 at 09:30
12

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

android:text='@{"Hello "+user.firstName}'
ArJ
  • 385
  • 5
  • 14
7

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);

Yogesh Rathi
  • 6,056
  • 4
  • 48
  • 76
7

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}'
kishan verma
  • 867
  • 15
  • 13
4

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>
Ready Android
  • 3,350
  • 1
  • 24
  • 39
4

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}'
Jackky777
  • 634
  • 11
  • 19
0

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}'
Aziz
  • 1,609
  • 16
  • 20
0

Probably late to the party: Below code will also work

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

madroid
  • 336
  • 4
  • 11
0

Few cents from me

If you want to check null and set default value try this answer

 android:text='@{(user.first_name ?? "") +" "+ (user.last_name ?? "")}'
Ranjithkumar
  • 14,780
  • 10
  • 107
  • 140