Not able to make click event in android data binding
In later versions of the DataBinding library you can just bind to your click event like this:
android:onClick="@{() -> viewModel.save()}"
Then, in your viewmodel you can just add a listener like this:
public void save(){
...
}
Make your MyHandlers
interface from class.
public class MyHandlers {
public void onShowToast(View view);
}
Implement it in your Activity
or Fragment
, in your case it will be as follows
public class View extends AppCompatActivity implements ViewContract.requiredMethods, MyHandlers{
ViewModel mModel;
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting the layout
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mModel=new ViewModel(getContext());
binding.setUser(mModel);
binding.setHandlers(this);
}
@Override
public void onShowToast(View view) {
Toast.makeText(view,"Clicked",Toast.LENGTH_SHORT).show();
}
}
Override your interface method onShowToast and set the handler for your binding, and that's it, you are done with click events