Search code examples
androidandroid-edittext

Android not allowing multi lines in `EditText` when filtering the emojis


I am not allowing any special chars and emojis using this extension function. Basically, this will not allow any emojis, symbols, and special characters other than what we pass in allowedChars.

fun EditText.filterEmojisAndDigits(allowedChars: String) {
    filters = arrayOf(InputFilter { source, _, _, _, _, _ ->
        source.filter {
            Character.getType(it) != Character.SURROGATE.toInt() &&
                    Character.getType(it) != Character.OTHER_SYMBOL.toInt() &&
                    allowedChars.contains(it, false)
        }
    })
}

This is I made the EditText

<EditText
        android:id="@+id/etAddComment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/dotted"
        android:gravity="center|start"
        android:hint="@string/enter_comments"
        android:digits="@string/supported_digits"
        android:importantForAutofill="no"
        android:inputType="textMultiLine"
        android:maxLength="950"
        android:maxLines="10"
        tools:text="Test data"
        android:minHeight="@dimen/dp_150"
        android:paddingStart="@dimen/dp_10"
        android:paddingEnd="@dimen/dp_10"
        android:textColor="@color/primary_text"
        android:textColorHint="@color/secondary_text"
        android:textSize="@dimen/sp_16" />

Where supported_digits are:

<string name="supported_digits">!"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?{|}~[\]^_`@ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz</string>

This is working fine but not allowing multiple lines. Any idea how can we support multi-lines along with filtering emojis?


Solution

  • Just added \n in the supported_digits then it worked.

    i.e.,

    <string name="supported_digits">!"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?{|}~[\]^_`@ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz\n</string>