Reputation: 23
android:password="true"
This hides the letters (****) but not immediately! When I type the letters, it take a while to hide.
For example, at the moment I type "a"; it shows ***a then it becomes ****. How can I transform it immediately?
Upvotes: 2
Views: 1582
Reputation: 2070
Implementation of TransformationMethod to hide letters when writing password:
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// example of usage
((TextView) findViewById(R.id.password)).setTransformationMethod(new HiddenPassTransformationMethod());
}
private class HiddenPassTransformationMethod implements TransformationMethod {
private char DOT = '\u2022';
@Override
public CharSequence getTransformation(final CharSequence charSequence, final View view) {
return new PassCharSequence(charSequence);
}
@Override
public void onFocusChanged(final View view, final CharSequence charSequence, final boolean b, final int i,
final Rect rect) {
//nothing to do here
}
private class PassCharSequence implements CharSequence {
private final CharSequence charSequence;
public PassCharSequence(final CharSequence charSequence) {
this.charSequence = charSequence;
}
@Override
public char charAt(final int index) {
return DOT;
}
@Override
public int length() {
return charSequence.length();
}
@Override
public CharSequence subSequence(final int start, final int end) {
return new PassCharSequence(charSequence.subSequence(start, end));
}
}
}
}
Upvotes: 0
Reputation: 2683
I believe this behaviour is intentional, as Android is used on smartphones with tiny little keyboards (physical and on-screen) where it's easy to make a typo. Displaying the letter briefly is so that the user can see if they typed something wrong, rather than hiding it and having no idea until they get an "incorrect password, your account has now been locked" type error!
I believe that the android:password="true" assigns a TransformationMethod to the text field which is responsible for converting the text into dots. I'm not an Android developer, but from reading the documentation I would imagine that this TransformationMethod has the delay built into the afterTextChanged callback. You could try writing your own TransformationMethod and play around with this and see if you can create your own version of the password masking rather than using the built-in one.
Just keep in mind the warnings in the doc, though, about avoiding infinite loops, because updating the text can re-trigger the events that you were notified about initially.
Upvotes: 4