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
557 stars 83 forks source link

Strange behavior when using layout_centerHorizontal #23

Open Logerfo opened 8 years ago

Logerfo commented 8 years ago

I've noticed the icon would not work if I set android:layout_centerHorizontal=true in the ShowHidePasswordEditText view, but I also observed that it would work (and apparently that's only way) if I swipe the icon from left to right.

PS: I'm using RelativeLayout, I don't know if this makes any difference.

Edit: I forgot to mention that I'm also using api 24.

joshkendrick commented 8 years ago

ive noticed the same thing. same issue described and same behavior with the swipe described. i can get it to work if i set the app:additionalTouchTargetSize sufficiently large (like 100dp), but im hesitant to do that because it seems hacky.

joshkendrick commented 8 years ago

i think something like this should fix it -- i think the problem stems from using getLeft() and getRight()

@Override
public boolean onTouchEvent(MotionEvent event) {
    if ( event.getAction() == MotionEvent.ACTION_UP && mVisibilityDrawable != null ) {
        Rect bounds = mVisibilityDrawable.getBounds();

        float clickX = event.getX();
        float iconXStart = mIsRightToLeft ? getWidth() - getTotalPaddingLeft() : getWidth() - getTotalPaddingRight();

        //check if the touch is within bounds of mVisibilityDrawable icon
        if ( iconXStart <= clickX && clickX < iconXStart + bounds.width() ) {
            togglePasswordVisibility();

            //use this to prevent the keyboard from coming up
            event.setAction(MotionEvent.ACTION_CANCEL);
        }
    }

    return super.onTouchEvent(event);
}
Logerfo commented 8 years ago

@joshkendrick I don't understand how this would explain the swipe behavior

joshkendrick commented 8 years ago

my issue was that the icon wouldnt work at all if my layout was android:layout_centerHorizontal=true -- that's what the above seems to have fixed for me.

swipe doesnt seem to be fixed, but im betting the swipe behavior is due to to this being checked on MotionEvent.ACTION_UP, (and not entirely related to any centerHorizontal stuff)

Logerfo commented 8 years ago

I see. So this is probably two (semi) separated issues. I cannot test it at the moment, but I guess the perfect pull request for that would get rid of both issues, since they are probably in the same piece of code. Does swiping actually trigger MotionEvent.ACTION_UP?