I want to create custom class which extends TextInputLayout
and have sort of an access into its inner EditText
, so I can set focus listener on it. Based on focus state and text in the EditText
the error shows in TextInputLayout
. Below is my current simple code where edit text is null. How can I achieve so have custom TextInputLayout
that can communicate with its inner EditText
?
class CustomTextInputLayout : TextInputLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
init {
Log.e("_TEST", this.editText?.toString() ?: "null") // gives null
editText?.setOnFocusChangeListener { v, hasFocus ->
if (hasfocus.not()) {
val textToCheck = editText?.text.toString()
this.error = someValidationFunction(textToCheck)
}
}
}
}
<com.app.utility.CustomTextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.app.utility.CustomTextInputLayout>