Minimal Android MVVM Databinding Setup with Kotlin
Posted onEdited on
Databinding for MVVM or any other pattern is a must have. Let’s see how to set it up. The reason for this blog is that it seems most of the examples out there online either deprecated or just try to solve another problem. Our goal is to setup a project with a brand new project generated from Android Studio.
First thing first, creating a new project from Android Studio.
What happens so far? In step 3, we use that viewModel variable in XML to do the binding. But it hasn’t been initialized yet. So here, we first setup the binding, then populate the binding.viewModel with an actual instance of the MainViewModel.
Run the app, it works now. You can see Albert on the screen
5. What about bind an event handler
Let’s see you want to bind a onClick for a button.