Reputation: 1074
I am trying to bind edittext onTextChanged
to the viewmodel.
this is my XML:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="mvvm.f4wzy.com.samplelogin.ui.login.viewmodel.LoginViewModel" />
</data>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:onTextChanged="@{viewmodel.onEmailChanged}"
android:paddingStart="5dp"
android:textColor="#383838"
android:textSize="20sp"/>
</layout>
LoginViewModel.kt
class LoginViewModel(application: Application) : AndroidViewModel(application) {
fun onEmailChanged(s: CharSequence) {
//some code
}
}
kapt gives me an error for on text changed and it says:
[kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors. ****/ data binding error ****msg:Cannot resolve type for viewmodel.onEmailChanged
I have tried android:onTextChanged="@{(s) -> viewmodel.onEmailChanged}"
and android:onTextChanged="@{viewmodel::onEmailChanged}"
but still same error.
any help?
Upvotes: 1
Views: 4618
Reputation: 2129
Why don't you just use two-way binding with a LiveData
property?
First, add the new email
property to your viewmodel and subscribe to it to receive the changes:
class LoginViewModel(application: Application) : AndroidViewModel(application) {
val email = MutableLiveData<String>()
// This observer will invoke onEmailChanged() when the user updates the email
private val emailObserver = Observer<String> { onEmailChanged(it) }
init {
email.observeForever(emailObserver)
}
override fun onCleared() {
email.removeObserver(emailObserver)
}
fun onEmailChanged(newEmail: String) {
// Some code
}
}
Then, update the view to create a two-way binding between the EditText
and the property email
of your viewmodel:
<EditText
...
android:text="@={viewModel.email}" />
Thanks to the two-way binding, whenever the user updates the text in the view, your viewmodel property will update (and vice versa).
This should hopefully do the trick!
Upvotes: 5