Search code examples
androidandroid-edittext

In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?


I am trying to show in asterisk (*) symbol in place of dots in EditText having inputType as textPassword. I came across post that ask to use setTransformationMethod() and implement PasswordTransformationMethod. But which method I of that class need I implement and how show asterisk? Is there other way to do that?

Thanks


Solution

  • I think you should go through the documentation. Create your PasswordTransformationMethod class, and in the getTransformation() method, just return a string of * characters that is the same length as the contents of your password field.

    I did some fiddling and came up with an anonymous class that worked for me to make a field full of *s. I converted it into a usable class here:

    public class MyPasswordTransformationMethod 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
            }
        }
    };
    
    // Call the above class using this:
    text.setTransformationMethod(new MyPasswordTransformationMethod());