scottyab / showhidepasswordedittext

Show/Hide Password EditText is a very simple extension of Android's EditText that puts a clickable hide/show icon in the right hand side of the EditText that allows showing of the password.
Apache License 2.0
558 stars 83 forks source link

Custom Typefaces overwritten #14

Closed crysxd closed 8 years ago

crysxd commented 8 years ago

A typeface which is set using setTypeface(Typeface) is overwritten when tapping the icon. This is caused by the call to setInputType() in togglePasswordVisibility(). Instead of calling setInputType() it would be better to use setTransformationMethod() like this:

private void togglePasswordVisibility() {
    // Store the selection
    int selectionStart = this.getSelectionStart();
    int selectionEnd = this.getSelectionEnd();

    // Set transformation method to show/hide password
    if (isShowingPassword) {
        setTransformationMethod(new PasswordTransformationMethod());
    } else {
        setTransformationMethod(null);
    }

    // Restore selection
    this.setSelection(selectionStart, selectionEnd);

    // Toggle flag and show indicator
    isShowingPassword = !isShowingPassword;
    showPasswordVisibilityIndicator(true);
}

Benefits:

Tested on API 23

scottyab commented 8 years ago

Thanks for mentioning that potential solution. I'll take a look at fix. (duplicate of #7)

guillermomuntaner commented 8 years ago

Same solution; this is just the internal of TextView. A few notes:

Full EditText source as reference:

    /**
     * Set the type of the content with a constant as defined for {@link EditorInfo#inputType}. This
     * will take care of changing the key listener, by calling {@link #setKeyListener(KeyListener)},
     * to match the given content type.  If the given content type is {@link EditorInfo#TYPE_NULL}
     * then a soft keyboard will not be displayed for this text view.
     *
     * Note that the maximum number of displayed lines (see {@link #setMaxLines(int)}) will be
     * modified if you change the {@link EditorInfo#TYPE_TEXT_FLAG_MULTI_LINE} flag of the input
     * type.
     *
     * @see #getInputType()
     * @see #setRawInputType(int)
     * @see android.text.InputType
     * @attr ref android.R.styleable#TextView_inputType
     */
    public void setInputType(int type) {
        final boolean wasPassword = isPasswordInputType(getInputType());
        final boolean wasVisiblePassword = isVisiblePasswordInputType(getInputType());
        setInputType(type, false);
        final boolean isPassword = isPasswordInputType(type);
        final boolean isVisiblePassword = isVisiblePasswordInputType(type);
        boolean forceUpdate = false;
        if (isPassword) {
            setTransformationMethod(PasswordTransformationMethod.getInstance());
            setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
        } else if (isVisiblePassword) {
            if (mTransformation == PasswordTransformationMethod.getInstance()) {
                forceUpdate = true;
            }
            setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
        } else if (wasPassword || wasVisiblePassword) {
            // not in password mode, clean up typeface and transformation
            setTypefaceFromAttrs(null /* fontFamily */, -1, -1);
            if (mTransformation == PasswordTransformationMethod.getInstance()) {
                forceUpdate = true;
            }
        }

        boolean singleLine = !isMultilineInputType(type);

        // We need to update the single line mode if it has changed or we
        // were previously in password mode.
        if (mSingleLine != singleLine || forceUpdate) {
            // Change single line mode, but only change the transformation if
            // we are not in password mode.
            applySingleLine(singleLine, !isPassword, true);
        }

        if (!isSuggestionsEnabled()) {
            mText = removeSuggestionSpans(mText);
        }

        InputMethodManager imm = InputMethodManager.peekInstance();
        if (imm != null) imm.restartInput(this);
    }
scottyab commented 8 years ago

Thanks @crysxd and @guillermomuntaner using PasswordTransformationMethod solves custom font issue and when using InputType.TYPE_NUMBER_VARIATION_PASSWORD. This will be in the next version 0.7