Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable emoji from being entered in Android EditText?

Most implementations of the text inputType (other than URI, password, etc.) for EditText and TextView allow Emoji - although in most Google keyboard configurations this button is hidden. Is there a way to disable Emoji from being entered in an EditText? Is there an inputType parameter that could be paired with textMultiLine that would disable Emoji?

like image 221
Alex Wang Avatar asked Apr 10 '14 14:04

Alex Wang


People also ask

How do I turn off Android Emojis?

Open any chat on any messenger app so the emoji bar will appear. Start typing and once you see the emoji bar, swipe left on it. You will see a Remove Bar button, tap on it and it will take you to settings. Here you can disable the Emoji fast-access bar toggle to disable that emoji bar completely.


4 Answers

Modify build.gradle file, add XEditText to your project:

dependencies{
    compile 'com.xw.repo:xedittext:2.0.0@aar'
}

after that, in your layout.xml:

<com.xw.repo.XEditText
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:x_disableEmoji="true"/>

Or:

Customize EditText like this:

public class CustomEditText extends EditText {
    public CustomEditText(Context context) {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setFilters(new InputFilter[]{new EmojiExcludeFilter()});
    }

    private class EmojiExcludeFilter implements InputFilter {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                int type = Character.getType(source.charAt(i));
                if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
                    return "";
                }
            }
            return null;
        }
    }
}

Both will work well !

like image 190
woxingxiao Avatar answered Oct 04 '22 04:10

woxingxiao


There is tricky way for disabling emoji from keyboard..

you just have to set

android:inputType="textEmailAddress"

for EditText..

  <EditText
    android:id="@+id/edt_note"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/note"
    android:inputType="textEmailAddress"
    android:padding="10dp"
    android:textColor="@color/white" />

I am not sure that it will work in all cases but in my case it worked for me...

like image 22
Pragnesh Ghoda シ Avatar answered Oct 04 '22 02:10

Pragnesh Ghoda シ


There is value digits available in xml file for EditText. You can set there all acceptable chars.

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:digits="qwertyuiopasdfghjklzxcvbnm 1234567890 QWERTYUIOPASDFGHJKLZXCVBNM" />

I know, it's not the best solution but works :)

like image 21
Adrian Grygutis Avatar answered Oct 04 '22 03:10

Adrian Grygutis


Add this emoji filter class:

public class EmojiFilter {
    public static InputFilter[] getFilter()
    {
         InputFilter EMOJI_FILTER = new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                for (int index = start; index < end; index++) {

                    int type = Character.getType(source.charAt(index));

                    if (type == Character.SURROGATE || type==Character.NON_SPACING_MARK
                            || type==Character.OTHER_SYMBOL) {
                        return "";
                    }
                }
                return null;
            }
        };
         return new InputFilter[]{EMOJI_FILTER};
    }
}

And to disable emoji in edit text, do this:

editText.setFilters(EmojiFilter.getFilter());

There were some emoji which were able to type so i added:

type==Character.NON_SPACING_MARK || type==Character.OTHER_SYMBOL

in the if condition.

like image 14
Suraj Vaishnav Avatar answered Oct 04 '22 02:10

Suraj Vaishnav