-4

Looking to disable the .,?, 1,2 etc on the android keyboard.

I was thinking if the key is entered, run a method that converts it from "." to "" for example.

Or just straight up disable them from being entered.

How do I go about this?

Shevliaskovic
  • 1,562
  • 4
  • 26
  • 43
  • 2
    What's your use-case? There are several InputFilters which already exist (e.g. telephone numbers, which prevents letters). These **ignore** input from certain keys, they do not (cannot) disable the key on the keyboard itself. – ataulm Jul 13 '14 at 19:34

1 Answers1

2

You can't disable the keys on the soft keyboard like that... the closest to that functionality would be specifying inputType for the EditText but obviously that only works with things like number, and you can't do that for a specific custom set of keys like the set you mentioned.

Instead look into InputFilter, as explained here: How do I use InputFilter to limit characters in an EditText in Android?

Specifically the second answer (with modified if statements to fit your requirements):

new 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 (currChar=='.' || currChar=='?' || Character.isDigit(currChar) && !Character.isSpaceChar(currentChar)) {    
                 sourceAsSpannableBuilder.delete(i, i+1);
             }     
        }
        return source;
    } else {
        StringBuilder filteredStringBuilder = new StringBuilder();
        for (int i = start; i < end; i++) { 
            char currentChar = source.charAt(i);
            if (currChar=='.' || currChar=='?' || Character.isDigit(currChar) && !Character.isSpaceChar(currentChar)) {    
                filteredStringBuilder.append(currentChar);
            }     
        }
        return filteredStringBuilder.toString();
    }
}
}
Community
  • 1
  • 1
u3l
  • 3,342
  • 4
  • 34
  • 51