Reputation: 11688
It is typical to set the inputType=textPassword
for fields where users would type in anything that is at risk, like...a password. And when the user types it, each character they type is briefly flashed to let the user know what was actually entered and then turns into an asterisk so that casual observers (spies) have a lot more difficulty seeing what is typed.
Generally this works fine.
I have a specific case where for security reasons the character must NOT be flashed. Instead I want to simply display the asterisk.
I could handle each tap individually via setOnKeyListener()
, but it seems like this should be easy. However I don't see any built-in solution. Please tell me that I'm missing something.
Upvotes: 2
Views: 1425
Reputation: 11688
Thanks for all those who commented, prodded, and pointed out excellent tips. Using these valuable assists I was able to do something that was actually pretty easy and seems to work perfectly for this situation.
The trick is to override PasswordTransformationMethod and force your EditText to use this new one. The original charAt()
method uses Spannable that waits a second and half before hiding it. By overriding that method your code can do something much simpler.
Here's an example of the custom class (in kotlin)
class ReallyHideMyPassword : PasswordTransformationMethod() {
companion object {
const val HIDE_CHAR = '*'
}
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 = HIDE_CHAR
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return source.subSequence(startIndex, endIndex)
}
}
}
And to use this in your EditText:
val et = findViewById<EditText>(R.id.my_password_edittext)
et.transformationMethod = ReallyHideMyPassword();
Turns out that this is similar to https://stackoverflow.com/a/23110194/624814 (thanks Per.J!), but now you have it in kotlin. It's so similar that this question will probably be closed as too similar. But the wording of the question should help people find the answer more easily than the other post.
Upvotes: 5
Reputation: 1937
You are not missing anything, I've struggled with this in the past and until now I've found no built-in solution, I've used addTextChangedListener
to do this and external libraries.
Disabling this setting does exactly what you asked, and it applies to every app but I guess you only can change this by code on a rooted phone.
I know this is no solution but it's a statement that this isn't available for developers and we have to make our own thing.
EDIT: The comments offers great solutions but as you said, not built-in.
Upvotes: 1