1

I am appending "-" in edittext when text length is 3 or 7 .

Following is my code.

        @Override
        public void afterTextChanged(Editable editable) {

            if(editable.length() == 3 || editable.length() == 7){
                editable.append("-");
            }

This works fine, and adds "-", but when i try to remove some text, it stuck on these places, so if i enter 221- and then i press backspace it does not work, and does not allow me to remove these characters.

dev90
  • 7,187
  • 15
  • 80
  • 153

1 Answers1

0

It's because when you press backspace with 221-, you are removing the - character which gives you 221. But in your text changed listener, it appends another - since you're left with 3 characters so your text becomes 221- again. Therefore you're going in a loop. You should perform the text formatting at the end to avoid this. Or you can add another listener to detect when backspace is pressed.

See this link for more details Android EditText delete(backspace) key event

Christilyn Arjona
  • 2,173
  • 3
  • 13
  • 20