Data binding button does not work

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

+9
source share
2 answers

I think there is a mistake in your activity declaration. Anyway, you are still not setting your callback as such:

binding.setCallback(this);

or

binding.setCallback(actionCallback);

+21
source

Purely the answer is correct, but I solved this problem

using this.

 binding.setHandlers(this); 

I worked in a fragment. Maybe this helps someone.

0
source

Source: https://habr.com/ru/post/1263585/


All Articles