onClick in Button do not work use Data Binding
You forgot to set the ViewModel to your binding.
If you expect the onClick
to be received in your activity, you have to do
binding.setPresenter(this)
although I would recommend calling the ViewModel in your XML file viewModel
or activity
(it's called presenter right now).
If you want your presenter
ot receive the onClick
, change the ViewModel type in your layout from activity to presenter, implement the onClick
method in your presenter, and do
binding.setPresenter(presenter)
Best way to do this is receive the onClick in the activity itself then pass that event into the function (if any) in the view model. You can do that in the following steps
1- In layout file, make change as:-
android:onClick="onClick"
2- In activity file, implement View.OnClickListener and register listener as binding.test.setOnClickListener(this)
3- Override the onClick method as
@Override
public void onClick(View view) {
if(view == binding.test){
//any utility method calls then calling the fun of view model
(yourViewModel).onHistoryClick();
}
}