Amira Elsayed Ismail
Amira Elsayed Ismail

Reputation: 9394

how to avoid characters like �‎ in an edittext?

I have an issue when user try to enter text copied that this text sometimes contains some special characters like �

and this make JSON string to be not formated, so please how can I avoid user enter such characters

please take into consideration that user can enter Arabic text and English text only

Upvotes: 0

Views: 146

Answers (1)

dalla92
dalla92

Reputation: 442

Try using InputFilters on the Edittext:

InputFilter filter = new InputFilter() {
        @Override
        public CharSequence filter(CharSequence source, int start, int end,
                                   Spanned dest, int dstart, int dend) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = start; i < end; i++) {
                if (isEnglishOrArabicChar(source.charAt(i))) {
                    stringBuilder.append(source.charAt(i));
                }
            }
            return stringBuilder.toString();
        }
    };
etName.setFilters(new InputFilter[]{filter});

private static boolean isEnglishOrArabicChar(char c) {
    Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
    return ub == Character.UnicodeBlock.ARABIC || ub==Character.UnicodeBlock.BASIC_LATIN;
}

Reference

Upvotes: 3

Related Questions