Part 3 – Expression Syntax 1/2

fragment_example.xmlstrings.xmlExampleFragment.ktactivity_main.xmlMainActivity.ktUser.kt
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <import type="android.view.View" />

        <import type="com.codinginflow.databindingexample.User" />

        <variable
            name="user"
            type="User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".ExampleFragment">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{@string/hello(user.firstName, user.lastName)}"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_first_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.firstName, default = `First Name`}"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_last_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{user.lastName, default = "Last Name"}'
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/text_view_age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(user.age) + ` years old`}"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textColor="@{user.age &lt; 18 ? @android:color/holo_red_light : @android:color/black}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="You are not old enough to use this app"
            android:visibility="@{user.age > 17 ? View.GONE : View.VISIBLE}" />

    </LinearLayout>
</layout>
<resources>
    <string name="app_name">Data Binding Example</string>
    <string name="hello">Hello, %1$s %2$s</string>
</resources>
package com.codinginflow.databindingexample

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.codinginflow.databindingexample.databinding.FragmentExampleBinding

class ExampleFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = FragmentExampleBinding.inflate(inflater,
            container, false)

        binding.user = User("Florian",  "Walther", 17)

        return binding.root
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
package com.codinginflow.databindingexample

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        supportFragmentManager.beginTransaction().replace(R.id.fragment_container,
            ExampleFragment()).commit()
    }
}
package com.codinginflow.databindingexample

data class User(val firstName: String, val lastName: String, val age: Int)