Programmatically setting up password dots in EditText

You don't need to create another password transformation method, you could do that.

password.setTransformationMethod(PasswordTransformationMethod.getInstance());

In onCreate() add these two lines while creating EditText

    et2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)
    et2.setTransformationMethod(new MyPasswordTransformationMethod());

then create MyPasswordTransformationMethod class in same activity like this

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
        }
    }
};

Tags:

Android