androidandroid-edittextandroid-input-filter

EditText input filter causing repeating letters


I have been limiting the input to my edittext like this;

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            String output = "";
            for (int i = start; i < end; i++) {
                if (source.charAt(i)!='~'&&source.charAt(i)!='/') {
                    output += source.charAt(i); 
                }
            } 
            return output;
        }
    };

But anyone who has used this method will know that it causes repeating characters when it is mixed with auto correct and the backspace key. To solve this I removed the auto correct bar from the keyboard like this;

Edittect.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

Now this works fine on the stock android keyboard, but the problem is on alternative keyboards(from Google play) it does not disable the auto correct, and so therefore I run into the problem of repeating characters again. Has anyone encountered this/know how to solve it?


Solution

  • EDIT - This doesn't quite work. On some devices(it seems like most samsungs) the repeating letter problem comes back - just slightly less often.

    I would seriously recommend finding a different way to limit inputs, because the input filter has some serious problems as of the moment.

    I suggest the following:

    I figured the problem out - this is what I used in the end

    InputFilter() {
    @Override
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
    
        if (source instanceof SpannableStringBuilder) {
            SpannableStringBuilder sourceAsSpannableBuilder = (SpannableStringBuilder)source;
            for (int i = end - 1; i >= start; i--) { 
                char currentChar = source.charAt(i);
                 if (currentChar=='/' || currentChar=='~') {    
                     sourceAsSpannableBuilder.delete(i, i+1);
                 }     
            }
            return source;
        } else {
            StringBuilder filteredStringBuilder = new StringBuilder();
            for (int i = 0; i < end; i++) { 
                char currentChar = source.charAt(i);
                if (currentChar != '~'|| currentChar != '/') {    
                    filteredStringBuilder.append(currentChar);
                }     
            }
            return filteredStringBuilder.toString();
        }
    }
    }