EditText is deleted when I use setSelection () within the onTextChanged


I'm trying to put a phone mask using the onTextChanged method, the text received in this method is coming back, and when I try to put the setSelection () method so that the cursor positions at the end, my EditText is off, in the Galaxy Tab 10. How to solve this?

private TextWatcher filterTextWatcherTelefone = new TextWatcher() {
        public void afterTextChanged(Editable s) {


        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            try {
                if (atualizando) {
                    atualizando = false;

                String resultado = limparFormatacaoNumero(s.toString());

                if (isNumero(resultado)) {

                    if (resultado.length() <= 14) {
                        resultado = adicionarFormatacaoTelefone(resultado);

                    } else {
                        resultado = resultado.substring(0, 14);
                        resultado = adicionarFormatacaoTelefone(resultado);
                    atualizando = true;

            } catch (Exception e) {
asked by anonymous 10.11.2014 / 11:37

2 answers


A great solution would be to deploy an already Android class, the PhoneNumberFormattingTextWatcher : / p>

PhoneNumberFormattingTextWatcher mPhoneWatcher = new PhoneNumberFormattingTextWatcher();       

Just a detail about this class

It defaults to% default%, that is, if the user's device is in English, the formatted number will be in English.

In the above documentation, you are saying that you accept the Locale constructor, but if you open javadoc this constructor has a PhoneNumberFormattingTextWatcher(String countryCode) tag, ie it is not available in the SDK to use.     

10.11.2014 / 14:20

I solved the problem by putting in EditText via xml the tag:


I discovered that the problem was self-completing.

11.11.2014 / 11:56