12

I want to hide emojis and auto suggestions from keyboard programmatically. Its working in some Android devices but not in all devices. here's my code for hide auto suggestions:

txtSingupemail.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS 
                           |InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtSignuppwd.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
txtSignuppwd.setTransformationMethod(PasswordTransformationMethod.getInstance());

Here's the snapshot of my UI:

enter image description here

This is layout when user clicks signIn button. When user tap on bottom left icon which is marked red, the keyboard height goes increase due to emojis as suggestions.

See below snapshot:

enter image description here

Is there any way to hide those top emojis from keyboard programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ruchir
  • 1,086
  • 4
  • 24
  • 48

9 Answers9

27

Try this it's works for me

editText.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;
        }
    }
Anjali-Systematix
  • 1,531
  • 14
  • 17
6

There are hundreds, if not thousands, of input method editors (a.k.a., soft keyboards) for Android.

None have to offer emoji. Those that do are welcome to offer them however they want, whenever they want.

There is no requirement for an input method editor to honor any flags that you put on the EditText. Therefore, there is no requirement for an input method editor to offer any means of blocking emoji input. And, even if some do offer this ability, others might not, and those that do might do so via different flags.

The decision of whether to have an emoji option on the keyboard is between the developers of the keyboard and the user (who chooses the keyboard to use). You do not get a vote.

Since AFAIK emoji are just Unicode characters, and since you should be supporting Unicode characters elsewhere (e.g., Chinese glyphs), it is unclear what technical reason you would have to avoid emoji. That being said, you are welcome to attempt to filter them out of the text being entered (e.g., use TextWatcher), if you are opposed to emoji.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    this is the best answer, a simple TextWatcher that worked great for me is the EmojiExcludeFilter found at http://stackoverflow.com/questions/22990870/how-to-disable-emoji-from-being-entered-in-android-edittext – CSmith Apr 07 '17 at 20:28
  • Using TextWatcher to filter out every emoji entered is current best solution. – Hitchhiker Aug 11 '17 at 12:24
  • 1
    Great answer, but, IMO, there is at least one technical reason even working in Unicode (and I've faced it several times): uploading user's data to a MySQL database in utf8, since it doesn't accept emojis, as explained [here](https://mathiasbynens.be/notes/mysql-utf8mb4). Sometimes, we don't have any control over the database, so blocking emojis is the only way to go. – thelawnmowerman Dec 18 '17 at 09:55
  • @thelawnmowerman: Then that is a matter of data validation at the app level. Personally, I have not tried [the answer that uses an `InputFilter`](https://stackoverflow.com/a/45795843/115145), but that's the general direction to head towards. – CommonsWare Dec 18 '17 at 11:57
4

It's probably not the best solution but the code below worked for me just fine:

editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

and since the password input type changes the font you just set the default typeface to it:

editText.setTypeface(Typeface.DEFAULT);
Alban Gashi
  • 603
  • 4
  • 14
1

For Android


mEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD| TEXT_MULTILINE); 

TYPE_TEXT_VARIATION_VISIBLE_PASSWORD - We use Password char as Text input for EditTest. It doesn't have emojis and email keys like .com and @ keys TEXT_MULTILINE - This will change the keyboard layout button [Done] or [->] to [Enter] key so we can use multi line text or new line feature.

In Xamarin Form

Create a CustomRender and in OnElementChanged method


protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

                if (Control != null)
                {
                    Control.ImeOptions = Android.Views.InputMethods.ImeAction.Done;
                    Control.InputType = Android.Text.InputTypes.ClassText | Android.Text.InputTypes.TextVariationVisiblePassword| Android.Text.InputTypes.TextFlagMultiLine;
                    Control.SetTypeface(Typeface.Default, TypefaceStyle.Normal);
                }
            }
Gladis Wilson
  • 8,400
  • 1
  • 12
  • 12
0

This might be helpful for you it will disable emojis android:inputType="textShortMessage" . Please let me know if it doesn't help you

Srikanth104
  • 65
  • 2
  • 10
0

In Kotlin, add Input filter to Edittext. I faced issues when digits and alphabets were rapidly typed. This code solves that

editText.filters = editTextAllowAlphabetsSymbols("") // Add any symbols that you wish to allow

then this

fun editTextAllowAlphabetsSymbols(symbols:String):Array<InputFilter>{
        return arrayOf(AlphabetsSymbolsInputFilter(symbols))
}

and finally

class AlphabetsSymbolsInputFilter(symbols:String) : InputFilter {

private var mWordPattern: String
var mLetterPattern:String

init {
    mLetterPattern = "[a-zA-Z.$symbols ]"
    //mLetterPattern = "[a-zA-Z0-9.$symbols ]" // replace if alphanumeric
    mWordPattern = "$mLetterPattern+"
}

override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
    if(source == ""){
        println("In backspace")
        return source
    }
    if(source.isNotEmpty() && source.toString().matches(mWordPattern.toRegex())){
        return source
    }
    var sourceStr = ""
    if(source.isNotEmpty() && !source.toString().matches(mLetterPattern.toRegex())){
        sourceStr = source.toString()
        while(sourceStr.isNotEmpty() && !sourceStr.matches(mWordPattern.toRegex())){
            println(" source --> $source dest ---> $dest")
            if(sourceStr.last().isDigit()) {
                print("Is digit ")
                sourceStr = sourceStr.subSequence(0, sourceStr.length - 1).toString()
            }
            else if(!sourceStr.last().toString().matches(mLetterPattern.toRegex())) {
                print("Is emoji or weird symbols")
                sourceStr = sourceStr.subSequence(0, sourceStr.length - 1).toString()
            }else
                break
        }
        return sourceStr
    }
    return source
   }
}
abitcode
  • 1,420
  • 17
  • 24
0

There is a slight issue with Anjali-Systematix's answer. If user types an emoji after any text without giving any space, like - "codebase", the text "codebase" gets removed too. This worked for me :

class EditTextForName : AppCompatEditText {
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        filters = arrayOf(EmojiExcludeFilter())
    }

    private inner class EmojiExcludeFilter : InputFilter {
        override fun filter(
            source: CharSequence,
            start: Int,
            end: Int,
            dest: Spanned,
            dstart: Int,
            dend: Int
        ): CharSequence? {
            for (i in start until end) {
                val type = Character.getType(source[i])
                if (type == SURROGATE.toInt() || type == OTHER_SYMBOL.toInt()) {
                    return source.substring(0 until i)
                }
            }
            return null
        }
    }
}
Sergei Kozelko
  • 691
  • 4
  • 17
-1

Best method till now and works in good maaner in your xml write android:digits= "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklmnbvcxz@.,1234567890"

  • " ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklmnbvcxz@.,1234567890" – PRATEEK Feb 15 '23 at 09:07
  • 1
    Please [edit] (ideally using this info https://stackoverflow.com/editing-help ) to fix typos, instead of repeating subtly changed parts down here in the comments. Also, please explain the idea of your code. – Yunnosch Feb 15 '23 at 09:59
-3

Try This

There is nothing that will 100% disable emoji.You can only hide default emos of keyboard whatever if someone is using custom keyboard,you can't hide the emos of custom keyboard may be this will help you

editext.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43