52

Is there any way to change the password text from dot(.) to asterisk(*) .

Password is entering in edittext.

<EditText
        android:id="@+id/passWord1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="number"
        android:password="true"/>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Jithin Sunny
  • 3,352
  • 4
  • 26
  • 42

8 Answers8

84

Insert edittext in your xml file,

<EditText
    android:id="@+id/passWordEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:inputType="textPassword"/>

and your class file go on and get findViewById from edittext and implement for this,

EditText edittext = (EditText)findViewById(R.id.passWordEditText);
edittext.setTransformationMethod(new AsteriskPasswordTransformationMethod());

and This class implement for that,

public class AsteriskPasswordTransformationMethod 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
        }
    }
};

And If your code is Kotlin then you have to make separate java file then you have to use java with kotlin code.

Najib.Nj
  • 3,706
  • 1
  • 25
  • 39
  • 3
    Thanks, this works for me. I my case, I have to hide last 10 numbers, NOT all. So, I just have to override the method charAt(int index) to return '*' if index > 9 else return mSource.charAt(index). – toantran Jun 05 '13 at 08:45
  • 1
    Nice work mate! Do you know what is the default char? In my case I have to show the first 3 char and than just the default password masking char, NOT the '*'. – narancs May 10 '17 at 14:14
  • 1
    This removes the lapse where the actual text is displayed first before the 'password' character. Any idea how it will be retained? – sticky May 27 '17 at 17:05
  • Has anyone tried putting a log in charAt() method, I see that its been called thousands of times. – abhishek maharajpet Aug 05 '19 at 12:59
19
<EditText
        android:id="@+id/passWord1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:inputType="textPassword"//here is the change. check it once in your xml
        android:password="true"/>

In eclipse there will be hints when you click Ctrl + Space when you move cursor at android:inputType. Then you can see list of options. there you can select textPassword

If you want to see * in place of . then check this Android: Asterisk Password Field

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
10

Got answer with help of link posted by Ram kiran

text.setTransformationMethod(new AsteriskPasswordTransformationMethod());


public class AsteriskPasswordTransformationMethod 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
    }
}
};
Jithin Sunny
  • 3,352
  • 4
  • 26
  • 42
  • thanks, this is exactly what i needed! what if i want to make it display the character and then asterisk-ize it after user types in another character, would i use this method alongside a textwatcher interface? – chornge Aug 28 '17 at 22:40
  • 1
    But its not working with app:passwordToggleDrawable – kundan kamal Jun 13 '19 at 10:27
5

Kotlin

class AsteriskPasswordTransformationMethod : PasswordTransformationMethod() {

    override fun getTransformation(source: CharSequence, view: View): CharSequence {
        return PasswordCharSequence(source)
    }

    inner class PasswordCharSequence (private val source: CharSequence) : CharSequence {

        override val length: Int
            get() = source.length

        override fun get(index: Int): Char = '*'

        override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
            return source.subSequence(startIndex, endIndex)
        }
        
    }
    
}
kulikovman
  • 333
  • 4
  • 8
3

In Kotlin way:

class AsteriskPasswordTransformationMethod: PasswordTransformationMethod() {

override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
    return super.getTransformation(source, view)
}


 abstract inner class PasswordCharSequence(val source: CharSequence) : CharSequence {
     override val length: Int
         get() = source.length

     override fun get(index: Int): Char =
         '*'

    override fun subSequence(start: Int, end: Int): CharSequence {
        return source.subSequence(start, end) // Return default
    }
}

}`

Aydın Ahmed
  • 559
  • 5
  • 11
2
EditText.setTransformationMethod(new PasswordTransformationMethod());
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
Suresh
  • 701
  • 1
  • 6
  • 20
0

For android:inputType, there is a type of password.

TieDad
  • 9,143
  • 5
  • 32
  • 58
0

Try this

android:inputType="textPassword"       
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98