Sasank Sunkavalli
Sasank Sunkavalli

Reputation: 3964

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

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.

Upvotes: 139

Views: 93091

Answers (13)

Stephen
Stephen

Reputation: 10059

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

Upvotes: 0

Manisha Saini
Manisha Saini

Reputation: 61

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

Upvotes: 0

Saad Ahmed
Saad Ahmed

Reputation: 71

Concat with the Backtick symbol `
Example:

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

Upvotes: 4

Ranjithkumar
Ranjithkumar

Reputation: 18356

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

Upvotes: 1

madroid
madroid

Reputation: 334

Probably late to the party: Below code will also work

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

Upvotes: 0

Prakash
Prakash

Reputation: 7996

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>

Upvotes: 101

kishan verma
kishan verma

Reputation: 1000

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

Upvotes: 10

Azizur Rehman
Azizur Rehman

Reputation: 2113

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

Upvotes: 0

Ravi
Ravi

Reputation: 35549

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

Upvotes: 333

Yogesh Rathi
Yogesh Rathi

Reputation: 6499

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

Upvotes: 8

Jackky777
Jackky777

Reputation: 654

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

Upvotes: 5

Ready Android
Ready Android

Reputation: 3622

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>

Upvotes: 5

ArJ
ArJ

Reputation: 395

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

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

Upvotes: 13

Related Questions