There is a password visibility toggle button within TextInputLayout for InputType textPassword.
Is it somehow possible to catch toggle events?
I couldn't find any public methods for this
I looked at the source code of the TextInputLayout to find the type of the view of the toggle button. Its CheckableImageButton. Everything else is easy. You need to find the view iterating recursively over children of the TextInputLayout View. And then setOnTouchListener as @MikeM suggested in the comments.
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;
}
An alternative implmentation of findTogglePasswordButton
private View findTogglePasswordButton() {
return findViewById(R.id.text_input_password_toggle);
}
@MikeM. thank you for id