I use TextInputLayout
from this library:
implementation 'com.google.android.material:material:1.0.0'
I have following part in the layout:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/phoneNumberLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/phoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/label_phone_number"/>
</com.google.android.material.textfield.TextInputLayout>
I have Kotlin Android Extensions is enabled and I try to assign TextWatcher to phoneNumber
as follows:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
phoneNumber.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
Log.e("DialogFragment", "phoneNumber::onTextChanged()")
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
Log.e("DialogFragment", "phoneNumber current text: " + s)
}
})
}
Nothing happens when I start typing , hours wasted... Any help would be appreciated.
You are adding the textwatcher in onActivityCreated
which does not hold your root View. You should reference your phone(TextInputEditText) from View
inflated in onCreateView
or inside onViewCreated
as suggested by @Miha_x64. Your code is correct though but the problem is with the view reference.