Featured post

Marshmallow Features Point by Point

Android Runtime (“ART”) Improved application performance and lower memory overhead for faster  multi-tasking. Battery Doze...

Monday, 15 February 2016

AsteriskPassword in EditText for Android


In android, By default provide property android:inputType = "textPassword" for Password edit text. But It has minor issue like password character visible while we are typing.
To Restrict this, I have created class AsteriskPasswordUtils. here you can give any password patterns.


import android.text.method.PasswordTransformationMethod;
import android.view.View;

public class AsteriskPasswordUtils extends PasswordTransformationMethod
    @Override    public CharSequence getTransformation( CharSequence source, View view )
        return new PasswordCharSequence(source);

    private class PasswordCharSequence implements CharSequence
        private CharSequence mSource;

        public PasswordCharSequence( CharSequence source )
            mSource = source; // Store char sequence        }

        public char charAt( int index )
            return '*'; // This is the important part        }

        public int length()
            return mSource.length(); // Return default        }

        public CharSequence subSequence( int start, int end )
            return mSource.subSequence(start, end); // Return default        }


EditText etPassword = (EditText) findViewById(R.id.etPassword);
etPassword.setTransformationMethod(new AsteriskPasswordUtils());

Note : Don't forget to add android:inputType = "textPassword" in the edit text on xml.

No comments:

Post a Comment