Change Edittext Password Mask Character to Asterisk (*)

Change EditText password mask character to asterisk (*)

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.

how to show a dot or asterisk when the user enter a text in an EditText?

try below code

editText.setTransformationMethod(new PasswordTransformationMethod());

public class PasswordTransformationMethod 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
}
}

};

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

I think you should go through the documentation. Create your PasswordTransformationMethod class, and in the getTransformation() method, just return a string of * characters that is the same length as the contents of your password field.

I did some fiddling and came up with an anonymous class that worked for me to make a field full of *s. I converted it into a usable class here:

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; // 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
}
}
};

// Call the above class using this:
text.setTransformationMethod(new MyPasswordTransformationMethod());

Customize Edittext input character to image?

you need to extend PasswordTransformationMethod and use setTransformationMethod method of EditText.

edt.setTransformationMethod(new CustomPasswordTransformationMethod());

and paste this CustomPasswordTransformationMethod

class CustomPasswordTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}

private class PasswordCharSequence implements CharSequence {
private CharSequence source;
public PasswordCharSequence(CharSequence source) {
this.source = source;
}
public char charAt(int index) {
if(index>4) //your own condition, when you want to hide characters.
return 0x2022; // change this to bullets you want like '*' or '.'
return source.charAt(index);
}
public int length() {
return source.length();
}
public CharSequence subSequence(int start, int end) {
return source.subSequence(start, end);
}
}
}

Above code will write character as it is upto 5 character, after that it will print bullets in EditText.

Reference taken from this post

UPDATE

Finally here is your answer :

Spannable.Factory spannableFactory;
int lastIndex = -1;

spannableFactory = Spannable.Factory
.getInstance();

1. add addTextChangedListener in your EditText.

    mEditText.addTextChangedListener(watcher);

TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (start>4) {
mEditText.removeTextChangedListener(watcher);
mEditText.setText(getIconText(context, s, start));
mEditText.addTextChangedListener(watcher);
mEditText.setSelection(s.length());
}
}

@Override
public void afterTextChanged(Editable s) {

}
};

  1. Convert your drawable into Spannable

    public Spannable getIconText(Context context, CharSequence text, int index) {
    Spannable spannable = spannableFactory.newSpannable(text);
    if (index>lastIndex) {
    spannable.setSpan(new ImageSpan(context, R.drawable.bullet_point),
    index, index + 1,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    lastIndex=index;
    return spannable;
    }

Sample Image



Related Topics



Leave a reply



Submit