androidandroid-input-filter

Android EditText InputFilter


How to delete two character at once with the filter?

I am trying to apply a simple filter to an EditText.
It must work as follows:
(want to receive format: 1234 5678....)

===> EDITED: First part works well.

1) when there are 4 digits in EditText and I am entering the 5-th digit -
first must appear a space and then this digit.
2) And I need a reverse for this (during characters deletion) -
the space must be deleted with the 5-th digit.

What is wrong with my code?

editText.setFilters(new InputFilter[]{new DigitsKeyListener(Boolean.FALSE, Boolean.TRUE) {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        //  any chars except backspace
        if (!source.equals("")) {
            if (dest.length() == 4) {
                // here I must add a space and then the source
                // ===> EDITED:
                return " " + source;
                // return super.filter(" " + source, start, end + 1, dest, dstart, dend + 1);
            } // backspace entered
        } else {
            if (dest.length() == 6) {
                // here I must delete the 6-th character 
                // and the space before
                return super.filter(source, 0, 0, dest, 5, 6);
            }
        }

        return null;
    }
}});

Solution

  • Please use this

    public class CustomFormatWatcher implements TextWatcher {
    
        private int size;
    
        public CustomFormatWatcher(int size) {
            this.size = size;
        }
    
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
            char hyphen = ' ';
            char c = 0;
            if (editable.length() > 0) {
                c = editable.charAt(editable.length() - 1);
                if (c == hyphen) {
                    editable.delete(editable.length() - 1, editable.length());
                }
            }
            if (editable.length() > 0 && (editable.length() % size) == 0) {
                c = editable.charAt(editable.length() - 1);
                if (hyphen == c) {
                    editable.delete(editable.length() - 1, editable.length());
                }
            }
            if (editable.length() > 0 && (editable.length() % size) == 0) {
                c = editable.charAt(editable.length() - 1);
                // Only if its a digit where there should be a space we insert a hyphen
                if (Character.isDigit(c) && TextUtils.split(editable.toString(), String.valueOf(hyphen)).length <= 3) {
                    editable.insert(editable.length() - 1, String.valueOf(hyphen));
                }
            }
        }
    }
    

    and then use

    myEditText.addTextChangedListener(new CustomFormatWatcher());