140

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.

13 Answers 13

333

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

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah it worked. Thanks . I was using single inverted comma ('). Grave accent(`) worked for me.
@SasankSunkavalli glad to help you :) , always use grave accent for string when using DataBinding.
I don't get it. On my .xml this method, as well as any other methods on this page, is not even close to being functional. All it does is prints as : @{Hello ` + user.firstName}`
This is DataBinding?
Yes, @IgorGanapolsky. This falls under Android's databinding concept. Quick Reference: developer.android.com/topic/libraries/data-binding
101

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>

2 Comments

great! thnx wery
thats what I'm looking for :)
13

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

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

Comments

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}'

Comments

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

3 Comments

I know this would work, But i want to use the concat way
You can also use formatted string resources in the expression: android:text="@{@string/hello(user.firstName)}"
it's %1$s instead of %$1s... ;)
5

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>

Comments

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}'

Comments

4

Concat with the Backtick symbol `
Example:

android:text="@{product.price + `USD`}"

Comments

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 ?? "")}'

Comments

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}'

Comments

0

Probably late to the party: Below code will also work

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

Comments

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)}'

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.