I have a 3 files(Activity, ViewModel,XML)
I use MVVM and connect XML to ViewModel.
<?xml version="1.0" encoding="utf-8"?>
<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.example.MainViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/priceTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={viewModel.price}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
ViewModel.class
open class MainViewModel : ViewModel() {
val price = MutableLiveData<String>()
val priceTextViewWidth = MutableLiveData<Int>()
}
and in the activity, I connect XML to view model with
binding.viewModel = viewModel
I can update Textview with change price variable in View Model but I need to pass TextView width to priceTextViewWidth in ViewModel.