Reputation: 54322
I have a TextInputLayout
with PasswordToggle
for one of my password EditText
as given below,
<android.support.design.widget.TextInputLayout
android:id="@+id/password_input_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/text_input_height"
android:gravity="center"
app:passwordToggleDrawable="@drawable/password_visibility"
app:passwordToggleEnabled="true">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/register_password_edittext"
android:layout_width="match_parent"
android:layout_height="@dimen/appcompat_editText_height"
android:hint="@string/register_password"
android:imeOptions="actionNext"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
Now I also have TextWatcher
implemented for this password EditText
to handle validation like this,
passwordEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// Do validation here.
}
});
The problem is, when the user clicks on the eye-icon
of the TextInputLayout
my TextChangeListener
gets triggered for no reason and validation failed block gets executed unnecessarily.
How do I avoid this? How do I handle PasswordToggleIcon
click action?
Upvotes: 1
Views: 731
Reputation: 31
passwordEditText.addTextChangedListener(new TextWatcher() {
String textBeforeChange;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
textBeforeChange = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (!textBeforeChange.equals(s.toString)) {
// Do validation here.
}
}
});
If the text hasn't changed then no need to preform validation I suppose? Possibly I am missing the intent of your question. Cheers!
Upvotes: 3