Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing the last character of a password in an EditText

I have an app where people need to login with a password. I would like for only the last character typed to be shown, but all I seem to get is all chars dots or all chars visible.

I tried a few things:

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

and setting inputtype in the xml.

I have only tested on a galaxy s2 as that is the only android device at my office at the moment, so I don't know if the problem is only with my device.

edit: Just tested on an HTC Sensation from a colleague and it does work as intended on his phone, but the question remains how to get this same thing on the Galaxy S2?

like image 753
Youri Avatar asked Dec 22 '11 08:12

Youri


1 Answers

It's been almost 1.5 years since this was asked :P. But I had the same requirement and was successfully able to implement it. Pass an object of the MyTransformation class as a parameter in the setTransformationMethod and you're good to go :) Here's the code.

public class MyTransformation 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) {
        //This is the check which makes sure the last character is shown
            if(index != mSource.length()-1)
                return '•';
            else
                return mSource.charAt(index);
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
}

enter image description here

like image 186
AndyFaizan Avatar answered Sep 21 '22 15:09

AndyFaizan