How to use TransformationMethod in android

I want to create an EditText that accepts passwords. I want to hide the symbol as soon as it is printed. So, I used TransformationMethod . I am new to this, so I tried the following code.

 EditText editText = (EditText) findViewById(R.id.editText); editText.setTransformationMethod(new PasswordTransformationMethod()); private class PasswordTransformationMethod extends Transformation implements TransformationMethod { @Override public CharSequence getTransformation(CharSequence source, View view) { return "/"; } @Override public void onFocusChanged(View view, CharSequence source, boolean focused, int direction, Rect previouslyFocusedRect) { source = getTransformation(source, view); } } 

However he throws

 01-03 10:22:35.750: E/AndroidRuntime(2102): java.lang.IndexOutOfBoundsException 

I'm missing something. Any help would be appreciated.

+6
source share
1 answer

The above method has many errors.

So, I use the code that I use to convert passwords to dots.

Create a separate class in the same Java file as this,

 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; } public char charAt(int index) { return '.'; } public int length() { return mSource.length(); } public CharSequence subSequence(int start, int end) { return mSource.subSequence(start, end); // Return default } } }; 

The implementation is as follows:

 passwordEditText = (EditText) findViewById(R.id.passwordEditText); passwordEditText.setTransformationMethod(new MyPasswordTransformationMethod()); 
+6
source

All Articles