data binding kotlin andoid
findViewById<TextView>(R.id.sample_text).apply {
text = viewModel.userName
}
The following example shows how to use the Data Binding
Library to assign text to the widget directly in the layout file
. This removes the need to call any of the Java code shown above.
Note the use of @{} syntax in the assignment expression:
<TextView
android:text="@{viewmodel.userName}" />
Complete example....
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.myapp.data.ViewModel" />
</data>
<ConstraintLayout... /> <!-- UI layout's root element -->
</layout>