replace character inside TextWatcher in android

Fcoder picture Fcoder · Mar 27, 2013 · Viewed 10.1k times · Source

i use a TextWatcher to change pressed key value. my goal is that replace some characters while typing. for example when i type keys, if reached "S" character, replaces it with "a" character. my question is: should i do it in beforeTextChanged?? how? can anyone give me an example?

Answer

quantum apps picture quantum apps · Apr 26, 2016

I know that this post is a couple of years old, but both versions did not work for me and have build a hybrid between the two answers.

@Override
public void afterTextChanged(Editable editable) {    
    if (editable.toString().contains(",")) {
       Editable ab = new SpannableStringBuilder(editable.toString().replace(",", ""));
       editable.replace(0, editable.length(), ab);
    }
}