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;
}
};