Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 314
anujprashar Avatar asked Jul 22 '12 03:07

anujprashar


People also ask

What is the EditText attribute android inputType used for?

The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.

How did the developer hide the input by dots in the password edit text?

change the edittext input type property to text password in xml. Show activity on this post. As the original poster mentioned in a comment, calling setSingleLine (boolean singleLine) resets the EditText settings, so you'll have to call that first and after that set custom settings like the password InputType .

How can add only numbers in EditText in android?

Use android:inputType="number" in XML can also input float.


1 Answers

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());
like image 173
Cat Avatar answered Oct 03 '22 10:10

Cat