Search code examples
androidkotlinmvvmdata-bindingandroid-databinding

How to use DataBinding on EditViews


I've been trying to make a sign in and out app but I am still stuck at getting the text from EditViews by using bindings to check out the written data but every time I try this error comes out

****/ data binding error ****msg:Cannot find the setter for attribute 'android:text' with parameter type androidx.lifecycle.LiveData on android.widget.TextView. file:C:\Users\mehmet\Desktop\andfun-kotlin-sleep-tracker-with-recyclerview\app\src\main\res\layout\register_layout.xml

this is an example for the edit view in the layout

<EditText
        android:id="@+id/email_editText_Register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="@string/hint_email"
        android:importantForAutofill="no"
        android:inputType="textEmailAddress"
        **android:text="@{register.emailEditTextRegisterContext}"**
        app:layout_constraintEnd_toEndOf="@+id/editText2"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="@+id/editText2"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

and this the variable of it in kotlin using observer class

private val _emailEditTextRegisterContext = MutableLiveData<EditText>()
val emailEditTextRegisterContext: LiveData<EditText>
    get() = _emailEditTextRegisterContext

and this the method that I've used to get the text

 val emails = _emailEditTextRegisterContext.value?.text.toString()

Solution

  • You should use Use MutableLiveData<String>() instead of MutableLiveData<EditText>() since text is String.