Part 5 – Event Handling

fragment_example.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>

        <variable
            name="fragment"
            type="com.codinginflow.databindingexample.ExampleFragment" />

        <variable
            name="user"
            type="com.codinginflow.databindingexample.User" />
    </data>

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

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onTextChanged="@{() -> fragment.displayTyping()}" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{fragment::displayHelloWorld}"
            android:text="Display hello world" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="@{() -> fragment.displayHelloUser(user)}"
            android:text="Display hello user" />

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onCheckedChanged="@{(cb, isChecked) -> fragment.onCheckBoxClicked(isChecked)}"
            android:onLongClick="@{() -> fragment.onCheckBoxLongClicked()}" />

    </LinearLayout>
</layout>
package com.codinginflow.databindingexample

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
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", 29, "Flow")
        binding.fragment = this

        return binding.root
    }

    fun displayTyping() {
        Toast.makeText(activity, "Typing...", Toast.LENGTH_SHORT).show()
    }

    fun displayHelloWorld(v: View) {
        Toast.makeText(activity, "Hello, world!", Toast.LENGTH_SHORT).show()
    }

    fun displayHelloUser(user: User) {
        Toast.makeText(activity, "Hello, ${user.firstName}!", Toast.LENGTH_SHORT).show()
    }

    fun onCheckBoxClicked(isChecked: Boolean) {
        Toast.makeText(activity, "Checkbox checked: $isChecked", Toast.LENGTH_SHORT).show()
    }

    fun onCheckBoxLongClicked(): Boolean {
        Toast.makeText(activity, "Checkbox long-clicked", Toast.LENGTH_SHORT).show()
        return true
    }
}
<?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,
    val nickName: String? = null
)