I need to restrict android edittext to allow only alphanumeric characters along with the following characters: space, '.', '_' and '-'.
I have used the following code segmennt for that:
filters[1] = new InputFilter() {
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);
String strCurrentChar = String.valueOf(currentChar);
if (!Character.isLetterOrDigit(currentChar)
&& !".".equals(strCurrentChar)
&& !"_".equals(strCurrentChar)
&& !" ".equals(strCurrentChar)) {
sourceAsSpannableBuilder.delete(i, i + 1);
}
}
return source;
} else {
StringBuilder filteredStringBuilder = new StringBuilder();
for (int i = start; i < end; i++) {
char currentChar = source.charAt(i);
String strCurrentChar = String.valueOf(currentChar);
if (Character.isLetterOrDigit(currentChar)
|| ".".equals(strCurrentChar)
|| "_".equals(strCurrentChar)
|| " ".equals(strCurrentChar)) {
filteredStringBuilder.append(currentChar);
}
}
return filteredStringBuilder.toString();
}
}
};
Its working fine restricting all other characters except for the mathematical 'pi' symbol in google-indic keyboard. How can I restrict that also so that the requirement is perfectly fulfilled?