1

I am working on a project where in certain edittexts i want it to contain only alphabets(both small and caps) and white spaces.So i set it dynamically in code as follows:

txtoccupation.setFilters(new InputFilter[] {
                new InputFilter() {
                    public CharSequence filter(CharSequence src, int start,
                            int end, Spanned dst, int dstart, int dend) {
                        if(src.equals("")){ // for backspace
                            return src;
                        }
                        if(src.toString().matches("[a-zA-Z ]+")){
                            return src;
                        }
                        return "";
                    }


                }
            });

The above code works fine in my older android 2.6 phone.That is , when i type in anything other than alphabets and white spaces it wont be shown on the edittext.But when i try the above on a new kitkat device, the text disappears and is shown in the suggestions below.How do i fix this issue?

EDIT: I used an input filter

filter = new InputFilter() { 
            public CharSequence filter(CharSequence source, int start, int end, 
    Spanned dest, int dstart, int dend) { 
                    for (int i = start; i < end; i++) { 
                            if (!Character.isLetter(source.charAt(i))&&!Character.isWhitespace(source.charAt(i))) { 
                                    return ""; 
                            }
                            if(Character.isWhitespace(source.charAt(i))){
                                return " ";
                            }
                    } 
                    return null; 
            } 
    }; 
Achuthan M
  • 349
  • 1
  • 4
  • 17

3 Answers3

0

just add this line to the edittext(in your xml), you want to restrict to have alphabets only

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

if (currentString.toLowerCase().startsWith(searchString )) { your code }

android provides methods with strings like toLowerCase(),toUpperCase() etc

0

Another option is to use input filters like this:

    InputFilter filter = new InputFilter() { 
    public CharSequence filter(CharSequence source, int start, int end, 
            Spanned dest, int dstart, int dend) { 
            for (int i = start; i < end; i++) { 
                    if (!Character.isLetterOrDigit(source.charAt(i))) { 
                            return ""; 
                    } 
            } 
            return null; 
    } 
    }; 

    edit.setFilters(new InputFilter[]{filter}); 

Taken from How do I use InputFilter to limit characters in an EditText in Android?

Community
  • 1
  • 1
arlistan
  • 731
  • 9
  • 20