I want to automatically put a "-" after 6 letters in my EditText and I want the user can continue to write after the "-". (I want the user writes : 1234562 and it appears 123456-2 in the EditText).
But I don't know how to do this and if it is possible. If you can help me, thanks
Add a textwatcher.Then use the following code:
@Override
public void afterTextChanged(Editable text) {
if (text.length() == 6) {
text.append('-');
}
}
You can also use multiple condition in the if statement like:
if (text.length() == 3 || text.length() == 6) {
text.append('-');
}
you can also delete characters with this:
text.addTextChangedListener(new TextWatcher() {
int first = 0;
int second;
@Override
public void afterTextChanged(Editable s) {
second = first;
first = s.length();
if(text.length()==6 && first>second){
text.append("-");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With