0

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" />
Amit Desale
  • 1,281
  • 3
  • 16
  • 43
  • Possible duplicate of [How to restrict Edittext to some particular characters in android?](http://stackoverflow.com/questions/17113942/how-to-restrict-edittext-to-some-particular-characters-in-android) – Sufian Oct 13 '15 at 06:51

3 Answers3

2

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 });
Ritt
  • 3,181
  • 3
  • 22
  • 51
2

Avoid double quote and single quote use following code

public static InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned destination, int dstart, int dend) { String blockCharacterSet = "'\""; if (source != null && blockCharacterSet.contains(("" + source))) { return ""; } return null; } };

You can set filter to your edit text like below

editText.setFilters(new InputFilter[] { filter });

neel
  • 44
  • 5
0

You can use below code to restrict Double Quotes in EditText using Kotlin.

Step1: Create Global Function in your project which you can use in multiple EditText.

fun restrictDoubleQuote():InputFilter{
val blockCharacterSet = "'\""

return InputFilter { source, start, end, dest, dstart, dend ->
    if (source != null && blockCharacterSet.contains("" + source)) {
        ""
    } else null
} 
}

Step2: Call above function in your EditText by using below code

et_package_included.filters =arrayOf<InputFilter>(restrictDoubleQuote())
Nimesh Patel
  • 1,394
  • 13
  • 26