Get Emoji Count In String

Viewed 4311

I would like to find how many emojis the user has input into an EditText. If the user only enters emojis, and uses 3 or less, I want to be able to display that string within the app with a larger font.

Right now I did come across this post which does help detect if emojis are present in the string, but I have not been able to figure out how to count the number of emojis.

Detecting if a character in a String is an emoticon (using Android)

Does anyone know how I can get the emoji count from a String?

5 Answers

My approach to this was to import this library:

implementation 'com.vdurmont:emoji-java:4.0.0'

Then I created a utility method to get the length of a string counting emojis as 1:

fun getLengthWithEmoji(s: String): Int{
        var emojiCount = EmojiParser.extractEmojis(s).size;
        var noEmojiString = EmojiParser.removeAllEmojis(s);
        var emojiAndStringCount = emojiCount + noEmojiString.length;
        return emojiAndStringCount;
}

Generally to 'Get emoji count in string' I would use this line:

var emojiCount = EmojiParser.extractEmojis(s).size;

This accounts for all the latest emojis (depending on how up to date your library it). Check for some of the forks that others have made on the library as they in some cases have added missing emoji patterns.

try this

private TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
        editText.post(new Runnable() {
            @Override
            public void run() {
                if (length < 100) {
                    if (count > 0 && after <= 0)/*remove emoij*/ {
                        Log.i("MainActivity", "emoij -> down length");
                        length--;
                    } else if (count > after)/*remove text*/ {
                        Log.i("MainActivity", "text -> down length");
                        length--;
                    } else if (count == 0 && after > 1)/*emoij*/ {
                        Log.i("MainActivity", "emoij -> increase");
                        ++length;
                    } else if (count == 0 && after == 1)/*Text*/ {
                        Log.i("MainActivity", "text -> increase");
                        ++length;
                    } else if (count > 0 && after > 1) {
                        Log.i("MainActivity", "text -> increase");
                        ++length;
                    }
                    if (s.length() <= 0)
                        length = 0;
                    Log.w("MainActivity", " Length: " + length);
                } else {
                    if (count > 0 && after <= 0)/*remove emoij*/ {
                        Log.i("MainActivity", "emoij -> down length");
                        length--;
                    } else if (count > after)/*remove text*/ {
                        Log.i("MainActivity", "text -> down length");
                        length--;
                    }
                     Log.w("MainActivity", " Length: " + length);
                }

                if (length == 100) {
                    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(s.length())});
                } else {
                    editText.setFilters(new InputFilter[]{});
                }
            }
        });
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

`

The best way for me was codePointCount method

For example this method returns 1 if text value is "":

    fun getLengthWithEmoji(name: String): Int {
        return name.codePointCount(0, name.length)
    }
Related