activity_layout.xml
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <import type="android.view.View" /> <variable name="callback" type="com.buscom.ActionCallBack" /> </data> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/ll_oml" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/grey_50" android:orientation="vertical"> <android.support.design.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@{(v) -> callback.onClick(v)}" android:text="Menu" /> </android.support.design.widget.CoordinatorLayout> </LinearLayout> </layout>
ActionCallBack.java
This is the interface that I implement in MainActivity
public interface ActionCallback { void onClick(View view); }
MainActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); actionCallBack = new ActionCallBack() { @Override public void onClick(View view) { System.out.println("Call onclick method *****"); } } }
When I click onClick (), the method is not called, the note is displayed in the output, or no action is taken. But it works in the traditional way with onClickListener
source share