Display emoji/emotion icon in Android TextView

谁都会走 提交于 2019-11-27 19:51:29

You could also try to find the emoji using a regular expression: "[\ue415\ue056\ue057]", instead of comparing the bytes. 😺

Why do you want to embed the protected Apple emoji images in your application at all?

The Unicode standard includes 722 emoji that can be displayed by Android's default font just by entering the Unicode chars into an EditText field or TextView.

You can, in addition, use the following library (in folder "Java") to automatically convert popular emoticons like :-) to the corresponding Unicode emoji:

https://github.com/delight-im/Emoji

greg7gkb

It works fine if you convert the string to a char array and check each char, such as:

StringBuilder sb = new StringBuilder();
for (char curr : str.toCharArray()) {
    sb.append((SUPPORTED_EMOJI_SET.contains(curr)) ? convertCharToImgTag(curr) : curr);
}

where SUPPORTED_EMOJI_SET is just a set of chars, for example:

new HashSet<Character>() {{
    add('\ue415');
    add('\ue056');
    ...
}}

You could also do this with a regex but I believe the above would run faster.

Here, Please go through below solution :

Problem : Inside TextView instead of Emoji, String \ue415\ue056\ue057 is showing.

Root cause : In java or android, programmatically string representation of Emoji's you will get as \\ue415\\ue056\\ue057. But when you try to print same String in console or LogCat then escape character is removed and you will get string as \ue415\ue056\ue057 because of which root cause of this issue is not detectable.

Solution : To solve this issue, we need to handle escape character. I have created below method which solve this problem.

public static String getEmojiFromString(String emojiString) {

    if (!emojiString.contains("\\u")) {

        return emojiString;
    }
    String emojiEncodedString = "";

    int position = emojiString.indexOf("\\u");

    while (position != -1) {

        if (position != 0) {
            emojiEncodedString += emojiString.substring(0, position);
        }

        String token = emojiString.substring(position + 2, position + 6);
        emojiString = emojiString.substring(position + 6);
        emojiEncodedString += (char) Integer.parseInt(token, 16);
        position = emojiString.indexOf("\\u");
    }
    emojiEncodedString += emojiString;

    return emojiEncodedString;
}    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!