In android how to show asterisk (*) in place of dots in EditText having inputtype as textPassword?

Viewed 8121

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

3 Answers

In kotlin, you can do this.

editText.transformationMethod = object : PasswordTransformationMethod() {
     override fun getTransformation(
         source: CharSequence?,
         view: View?
     ): CharSequence {
         return PasswordCharSequence(super.getTransformation(source, view))
     }

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

        override fun get(index: Int): Char {
            //Only modify char '\u2022' if you want to keep the default behaviour
            return if(source[index] == '\u2022') '*' else source[index]
        }

        override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
            return source.subSequence(startIndex, endIndex)
        }
    }
}
Related