I have a TextInputLayout for password. I have added passwordToggleEnabled=true to toggle the password visibility. I need to capture the event when user toggles password visibility. How can I do this.
<android.support.design.widget.TextInputLayout
android:id="@+id/password_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
app:passwordToggleEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enter_new_password"
android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
In the source of the TextInputLayout the type of the view of the toggle button is CheckableImageButton. You just need to find the view iterating recursively over children of the TextInputLayout View. And then setOnTouchListener.
View togglePasswordButton = findTogglePasswordButton(mTextInputLayoutView);
if (togglePasswordButton != null) {
togglePasswordButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// implementation
return false;
}
});
}
private View findTogglePasswordButton(ViewGroup viewGroup) {
int childCount = viewGroup.getChildCount();
for (int ind = 0; ind < childCount; ind++) {
View child = viewGroup.getChildAt(ind);
if (child instanceof ViewGroup) {
View togglePasswordButton = findTogglePasswordButton((ViewGroup) child);
if (togglePasswordButton != null) {
return togglePasswordButton;
}
} else if (child instanceof CheckableImageButton) {
return child;
}
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With