Search code examples
androidandroid-layoutdata-bindingandroid-databinding

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.


Solution

  • 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