Wednesday, 1 June 2016

Password Visible and Invisible By Drawable Right Android


Step 1:

You have to add android:drawableRight="@drawable/show_password" in 
EditText


Step 2:


edtPassword.setOnTouchListener(new View.OnTouchListener() {
    @Override    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getRawX() >= (edtPassword.getRight() - 
edtPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds()
.width())) {

                if (edtPassword.getInputType() == (InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)) {

                    edtPassword.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    edtPassword.setInputType(InputType.TYPE_CLASS_TEXT |
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }

                return false;
            }
        }
        return false;
    }
});


you can use given svg (copy and save it as show_password.xml)

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="15dp"
        android:height="15dp"
        android:viewportWidth="36.0"
        android:viewportHeight="36.0">
    <path
        android:pathData="M18,6C8.06,6 0,18 0,18s8.06,12 18,12c9.94,0 18,
-12 18,-12S27.94,6 18,6zM18.02,25.98c-4.42,0 -8,-3.58 -8,-7.98c0,-4.41 3.58,
-7.98 8,-7.98s8,3.57 8,7.98C26.02,22.41 22.44,25.98 18.02,25.98zM18,15.01c-1.65,
0 -2.98,1.34 -2.98,3s1.34,3 2.98,3c1.65,0 2.98,-1.34 2.98,-3S19.65,15.01 18,15.01z"

        android:fillColor="#A1A1A1"/>
</vector>

No comments:

Post a Comment