material-components/material-components-android

TextInputEditText has no valueAttrChanged for compiling twoWayDatabinding

Open

#1,150 opened on Mar 26, 2020

View on GitHub
 (0 comments) (0 reactions) (0 assignees)Java (15,910 stars) (3,023 forks)batch import
Help wantedWidget: TextField

Description

Description: I can't use two way data binding with TextInputEditText when using a MutableLiveData Getting "Could not find event 'valueAttrChanged' on View type 'com.google.android.material.textfield.TextInputEditText'"

Expected behavior: I don't understand why that would be intended. If it is, how should I work around this issue?

Source code:

//ViewModel 
var amount = MutableLiveData<Double>()

//Adapters

@BindingAdapter("value")
fun setVal(editText: EditText, newVal: Double) {
    val currentValue = editText.text.toString()
    try {
        if (currentValue.isNotEmpty()) {
            if (java.lang.Double.valueOf(currentValue) != newVal) {
                val decimalFormat = DecimalFormat("#.##")
                val value: String = decimalFormat.format(newVal)
                editText.append("$value")
            }
        }
    } catch (exception: Exception) { //
        Timber.i(exception)// Do nothing
    }
}

@InverseBindingAdapter(attribute = "value")
fun getVal(editText: EditText) : Double {
    val currentValue = editText.text.toString()
    return currentValue.toDouble()
}`

//XML
<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/amount_text_input_field"
                style="@style/TextInputLayoutStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:hint="@string/amount"
                android:textColorHint="@color/dark_grey"
                app:hintEnabled="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/date_text_input_field">`

                `<com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/editText_amount"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="numberDecimal"
                    android:paddingStart="30dp"
                    app:value="@={viewModel.amount}">

                </com.google.android.material.textfield.TextInputEditText>

Android API version: 3.6.1

Material Library version: 1.1.0

Device: Pixel API 29

Thank you in advance

Contributor guide