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
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 "
int emojiCount = 0;
for (int i = 0; i < yourString.length(); i++) {
int type = Character.getType(yourString.charAt(i));
if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
emojiCount++;
}
}
return emojiCount/2;
Another approach would be to take advantage of EmojiCompat
. This code presumes you initialized EmojiCompat
when your app was starting up. The basic idea here is to have EmojiCompat
process your CharSequence
, inserting instances of EmojiSpan
wherever any emoji appear, and then examine the results, returning a count of the EmojiSpan
instances in the processed Spannable
.
public static int getEmojiCount(CharSequence charSequence) {
int count = 0;
CharSequence processed = EmojiCompat.get().process(charSequence, 0, charSequence.length() -1, Integer.MAX_VALUE, EmojiCompat.REPLACE_STRATEGY_ALL);
if (processed instanceof Spannable) {
Spannable spannable = (Spannable) processed;
count = spannable.getSpans(0, spannable.length() - 1, EmojiSpan.class).length;
}
return count;
}
Do not forget to add dependency in app gradle:
implementation 'androidx.emoji:emoji:1.1.0'