0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • Your json problem is probably unrelated to the char that are entered. Better show some code related to your problem if you want it solved. – Loïc Faure-Lacroix Nov 28 '17 at 13:54
  • @LoïcFaure-Lacroix the JSON problem is not my side, it is server side, the server API crash when user enter such characters, so I do not have any codes for this problem, I just want to avoid any characters except [Arabic Alphabet, English Alphabet, Numbers] – Amira Elsayed Ismail Nov 28 '17 at 13:56
  • 1
    Looks like an **encoding** problem. – Phantômaxx Nov 28 '17 at 14:04

1 Answers1

3

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

dalla92
  • 432
  • 2
  • 8