138

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
  • 34,851
  • 21
  • 122
  • 183
Sasank Sunkavalli
  • 3,864
  • 5
  • 31
  • 55

13 Answers13

330

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
  • 34,851
  • 21
  • 122
  • 183
99

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,852
  • 1
  • 19
  • 34
Prakash
  • 7,794
  • 4
  • 48
  • 44
13

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

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

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
  • 984
  • 15
  • 17
8

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,331
  • 4
  • 51
  • 81
5

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
  • 644
  • 12
  • 19
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,529
  • 2
  • 26
  • 40
4

Concat with the Backtick symbol `
Example:

android:text="@{product.price + `USD`}"
Saad Ahmed
  • 71
  • 5
1

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
  • 16,071
  • 12
  • 120
  • 159
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,976
  • 20
  • 23
0

Probably late to the party: Below code will also work

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

madroid
  • 336
  • 3
  • 12
0

Got the same issue, this is what worked for me, hope it helps!!

android:text='@{"$"+String.valueOf(bean.payment_for==1?bean.price:bean.tip_amount)}'

Manisha Saini
  • 51
  • 1
  • 7
0

activity_main,xml:

<TextView
        android:id="@+id/word_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/word_count"
        tools:text="3 of 10 story" />

string.xml:

<string name="word_count">%d of %d words</string>

MainActivity.xml:

binding.wordCount.text = getString(R.string.word_count, 3, 10)

Output:

enter image description here

Stephen
  • 9,899
  • 16
  • 90
  • 137