Android : Two way data binding calls on change of every character in editText

Viewed 141

I have an ediText, wherein the user enters a date. I have attached a two-way databinding for the same.

<EditText
    android:id="@+id/dob"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toRightOf="@+id/midline"
    app:layout_constraintTop_toBottomOf="@+id/doj"
    app:layout_constraintBaseline_toBaselineOf="@+id/dobTv"
    app:layout_constraintWidth_percent="0.4"
    android:layout_margin="20dp"
    android:text="@={Converters.convertDateToString(viewModel.dob)}"></EditText>

here as you see,i use, converters to convert the date to string and vice versa.

object Converters {

@InverseMethod("convertDateToString")
@JvmStatic
fun convertStringToDate(str : String?) : Date?{
    str?.let {
       
            val date: Date = SimpleDateFormat("dd/MM/yyyy").parse(it)
            return date;
        }
    
    return null;
}

@JvmStatic
fun convertDateToString(date : Date?) :  String {
    date?.let {
        
            val dateFormat: DateFormat = SimpleDateFormat("dd/MM/yyyy")
            return dateFormat.format(it)
      
}
    return "";
}

}

The converter function string to date is called every time, any text changes in the editText ie, it's called as the user types the text. Is there any way, we can call the binding only after the user finishes typing.

0 Answers
Related