single ViewDataBinding class By binding data (a model) to a ViewDataBinding the view will update Click listeners and 2-way binding can update the model from the view Data is bound using expressions and converted using BindingAdapters
Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val firstName = findViewById<TextView>(R.id.first_name) // "bind" the TextView to a value firstName.text = "Hugo" } }
{ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // "bind" the TextView to a value first_name.text ="Hugo" } }
view, e.g. a view model Works well with MVVM style architectures View model ≠ ViewModel, but it can be. Also: click listeners for actions that do not deal with model state
Somewhere globally, or per activity etc DataBindingUtil.setDefaultComponent(myComponent) // For a specific binding val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main, myComponent)
fields are very convenient for form input Architecture components ViewModel can also implement Observable https://developer.android.com/topic/libraries/data-binding/architecture
update, but doesn't expose mutability val price: LiveData<Price> = MutableLiveData() val couponCode = MutableLiveData<String>() // for two-way data binding }
// requires a cast to update, but doesn't expose mutability val price: LiveData<Price> = MutableLiveData() val couponCode = MutableLiveData<String>() // for two-way data binding } // In Activity.onCreate() val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main) binding.lifecycleOwner = this