Reputation: 3176
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?
Upvotes: 5
Views: 5809
Reputation: 694
fun String.lengthWithEmoji():Int{
var count=0;
this.forEach {
if(isEmoji(it))
count++
}
return (this.length-(count/2))
}
fun isEmoji(char:Char): Boolean {
val type = Character.getType(char)
return type == Character.SURROGATE.toInt()
}
Upvotes: 0
Reputation: 443
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)
}
Upvotes: 1
Reputation: 5298
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'
Upvotes: 7
Reputation: 1051
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) {
}
};
`
Upvotes: 0
Reputation: 3298
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.
Upvotes: 2
Reputation: 10727
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;
Upvotes: 5