Search code examples
androidandroid-edittext

How to restrict single quote and double quote in edittext


I use editext for chat. everything goes fine. but when i enter single quote then it display's ' and when i enter double quote then it display's " so i want to restrict these two charaters.

  <EditText
        android:id="@+id/editText_add_chat_afcdl"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:padding="13dp"
        android:textColor="@android:color/black"
        android:inputType="textMultiLine"
        android:maxLength="256"
        android:textSize="18sp"
        android:maxLines="5"
        android:layout_centerVertical="true"
        android:hint="@string/chatMsgHint"
        android:textColorHint="#C3C3C8"
        android:layout_toLeftOf="@+id/button_add_afcdl"
        android:background="@drawable/rounded_edittext" />

Solution

  • Use custom input filter and then set it to the editText.

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dend) {
            char[] chars = {'\'','"'};
            for (int i = start; i < end; i++) {
                if (new String(chars).contains(String.valueOf(source.charAt(index))) {
                    return "";
                }
            }
            return null;
        }
    };
    edit.setFilters(new InputFilter[] { filter });