where to put click listeners in mvvm android

   <Button
       android:id="@+id/btn_nw"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="@{() -> viewModel.testLoginModuleClicked()}"
       android:text="Login"/>

In ViewModel class

val loginClickEvent = SingleLiveEvent<Void>()
fun testLoginModuleClicked() {
    loginClickEvent.call()
}

in your activity/fragment class

 loginVM.loginClickEvent.observe(this, Observer {
        callMockApi()
    })

  1. Put the on click listeners in your activity/fragment and not in the view-model as listeners are still part of the view.

  2. Shared preference methods should not be called inside the view-model itself, instead, make your view-model call a class that would do the saving of information into the shared preference. In this case, I would recommend using repository pattern. Your view-model will now then call method x() from your repository, and method x() will now then do the saving of information through shared preference, local database or maybe through the cloud.