Search code examples
androidkotlinmvvmdata-bindingandroid-viewmodel

how to update textview, ontextchanged in editext using Databinding android


I have edit text which will get input from the user

<EditText
        android:inputType="number"
        android:id="@+id/etQuantity"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@drawable/button_background"
        android:backgroundTint="@color/green"
        android:hint="Quantity"
        android:padding="5dp"
        android:onTextChanged="@{inspect::onQuantityTextChanged}"
        android:textColor="@color/lightblack" />

 <TextView
            android:id="@+id/tvCalculatedTotal"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/button_background"
            android:backgroundTint="@color/green"
            android:hint="Total"
            android:inputType="number"
            android:padding="5dp"
            android:text="@={inspect.calculatedTotal}"
            android:textColor="@color/lightblack" />

I want to set that to text view onTextchanged in edit text

class InspectViewModel(repository: UserRepository) : ViewModel() {

var calculatedTotal: String = ""

 fun onQuantityTextChanged(
    s: CharSequence,
    start: Int,
    before: Int,
    count: Int
) {

    calculatedTotal= s.toString()
    }
}

I am getting the value but it is not updating the text view, I used live data but getting ANR


Solution

  • You got ARN because of this line android:text="@={inspect.calculatedTotal}"

    Use android:text="@={inspect.calculatedTotal}" for your EditText

    and android:text="@{inspect.calculatedTotal}" for your TextView

    While calculatedTotal is MutableLiveData