Search code examples
androidkotlinandroid-databinding

android:onClick attribute is not working through data binding


Here is my code for Fragment class.

class FragmentOne : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        // return inflater.inflate(R.layout.fragment_one, container, false)
        val binding: FragmentOneBinding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_one, container, false)
        return binding.root
    }

    fun onClicking(){
        Toast.makeText(activity, "You clicked me.", Toast.LENGTH_SHORT).show()

    }
}

And here is my code for Fragment XML.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".FragmentOne">

    <data>
        <variable
            name="clickable"
            type="com.example.fragmentpractise1.FragmentOne" />
    </data>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hola Gola"
        android:layout_marginTop="40dp"
        android:onClick="@{()-> clickable.onClicking()}"/>

    </LinearLayout>
</layout>

Now what I am trying to understand, why android:onClick is not showing any toast result. On pressing the button nothing happens. I can show toast by setting onClickListener on button id in Fragment class but unable to show toast via onClick attribute in XML using databinding.


Solution

  • You're calling clickable.onClicking() in xml which is not set yet. When you instantiate a data binding object, you probably have to set its variables as well (like clickable in your example)

    Set that variable after instantiation like this

    
        override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            // Inflate the layout for this fragment
            // return inflater.inflate(R.layout.fragment_one, container, false)
            val binding: FragmentOneBinding =
                DataBindingUtil.inflate(inflater, R.layout.fragment_one, container, false)
            binding.clickable = this // your fragment
            return binding.root
        }
    

    Also using v instead of () inside onClick is a bit more rational because that's a lambda in Java syntax receiving one view argument. I suggest to change it to below for more readability

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hola Gola"
            android:layout_marginTop="40dp"
            android:onClick="@{ v -> clickable.onClicking()}"/>