Data Binding onClick not working

The easiest way is to set the view model and calling the proper method in the View's onClick from the layout: Your xml:

<data>
    <variable
        name="viewModel"
        type="co.package.MyViewModel" />
</data>

<com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/my_id"
                        android:layout_width="@dimen/full_width"
                        android:layout_height="wrap_content"
                        android:onClick="@{() -> viewModel.doSomething()}" />

But if for any reason you need to call a method from your fragment or activity, the best suggestion is to create an interface to handle the method, implement the method and set it to the layout as follows: Your xml

<data>
    <variable
        name="myHandlers"
        type="co.package.MyHandlersListener" />
</data>

<com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/my_id"
                        android:layout_width="@dimen/full_width"
                        android:layout_height="wrap_content"
                        android:onClick="@{() -> myHandlers.doSomething()}" />

And within your Fragment or Activity you create the interface and then implement it: Your activity/fragment:

/* My Handler Methods */

interface MyHandlersListener {
    fun doSomething()
}

Then implement the listener, taking into consideration that the method something is defined and implemented within your activity/fragment class:

private val myHandlersListener: MyHandlersListener = object : MyHandlersListener {
    override fun doSomething() {
        something()
    }
}

And using databinding, you can set the handler into your layout class (this can be done within the onCreate or onCreateView method depending if you are using and activity or fragment respectively):

myBinding.myHandlers = myHandlersListener

In this way it works perfectly and you follow the guide given by Android's team: https://developer.android.com/topic/libraries/data-binding/expressions#method_references


Please check with below code:

You have written as to call on Click of image as :

 android:onClick="@{() -> gameViewModel.onClickItem(1,1)}"

Try to write as below and check again :

android:onClick="@{(v) -> gameViewModel.onClickItem(1,1)}"

As per the Guidance This is not the way to achieve the Architecture Principles we can work as below as per the MVVM Architecture: 1. Create an Interface 2. Define Interface as handler inside the Layout File as :

<variable
        name="handler"
        type="com.cityguide.interfaces.MustVisitItemListener"></variable>

3.Now we are using this handler to define onclick as :

android:onClick="@{(v) ->handler.onGalleryItemClick(v,currentPosition,photo)}"
  1. Implement the Handler with our java Class or Activity class before bind the Handler with View as below:
private MustVisitItemListener mItemListener;

mItemListener = new MustVisitItemListener() { };

5.Set the Interface handler with bind object as below:

mbinding.setHandler(mItemListener);