Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputLayout Password Toggle Listener

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>
like image 823
Nitesh V Avatar asked Nov 16 '25 07:11

Nitesh V


1 Answers

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;
}
like image 184
Rainmaker Avatar answered Nov 17 '25 22:11

Rainmaker