First name and Last Name in one textview data binding android

1.3k views Asked by At

How to concatenate string in data binding. I want to show both firstName and lastName is same TextView. Any help thanks in advance.

<data>

    <variable
        name="task"
        type="com.myapp.app.Task" />
</data>

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="6dp"
    android:drawablePadding="6dp"
    android:ellipsize="end"
    android:gravity="center"
    android:maxLines="1"
    android:text="@={task.assignedTo.firstName}"//how to include lastname
    android:textColor="@color/textBlack"
    android:textSize="14sp"
    tools:text="Saikiran" />

1

There are 1 answers

2
Ravi On

First of all there should not be = after @ because it indicates two-way binding and there is no meaning of two-way binding with TextView.

To concate last name you can use + operator

android:text="@{task.assignedTo.firstName + task.assignedTo.lastName}"

and to concate it with some custom String :

android:text="@{task.assignedTo.firstName + ` - ` + task.assignedTo.lastName}"