6

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?

4
  • 2
    I believe it is just how the manufacturer implements the system UI, and it just varies device by device, therefore I am pretty confident it is not something that can be change on the developer end.
    – Reed
    Dec 22, 2011 at 10:07
  • 1
    I believe this is a device-wide setting and not something you can change in an app. If you select "Visible Passwords" in the "Location and Security settings" menu you can turn this feature on or off.
    – ethan
    Dec 22, 2011 at 14:24
  • You will be surprised if check how your EditText for password in landscape orientation. :)
    – Jin35
    Dec 22, 2011 at 16:14
  • meh don't want to see my app in landscape, but guess its a normal thing then.
    – Youri
    Dec 23, 2011 at 15:04

3 Answers 3

19

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

6
  • i just gave up when it worked on most other devices except my own, but with this piece of code it even works on my device. thanks :)
    – Youri
    Sep 2, 2013 at 12:13
  • i get this error Cannot resolve this symbol 'MyTransformation'
    – mrid
    Nov 27, 2018 at 16:39
  • @mrid: bit more detail regarding the error would be helpful. Where exactly do you get this?
    – AndyFaizan
    Nov 28, 2018 at 8:55
  • @AndyFaizan i got it working. i was passing MyTransformation.GetInstance() instead of new MyTransformation()....is there a way to hide the last visible character after, say 1 second ?
    – mrid
    Nov 28, 2018 at 10:40
  • can someone translate this to kotlin?
    – Fugogugo
    Apr 2, 2020 at 6:47
2

Improving on AndyFaizan's answer, my solution doesn't show characters while using backspace. Without this (simple) change it'd be possible to reveal the whole password by deleting characters at the end of the input.

public class CustomPasswordTransformation extends PasswordTransformationMethod {
    boolean lastActionWasDelete = false;

    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        super.onTextChanged(s, start, before, count);
        this.lastActionWasDelete = before > count;
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence source;

        PasswordCharSequence(CharSequence source) {
            this.source = source;
        }

        public char charAt(int index) {
            //This is the check which makes sure the last character is shown
            if (!lastActionWasDelete && index == source.length() - 1) return source.charAt(index);
            return '•';
        }

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

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

As ethan already mentioned ages ago, the user can toggle the password character display behaviour in the system security settings.

To hide the password just use

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

the rest is taken care of by the operation system.

In my case i wondered about different behaviour on a variation of devices. After some investigation the security setting 'Visible Passwords' was turned off on the device which did not show the last character.

If you must ignore the system wide setting, AndyFaizan 's password transformation solution is still the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.