Display emoji/emotion icon in Android TextView

后端 未结 4 1628
暖寄归人
暖寄归人 2020-12-05 05:32

I have some problem with displaying emoji icon in Android TextView

First, I found a list of emoji icon in unicode at here: http://www.easyapns.com/category/just-for-

4条回答
  •  無奈伤痛
    2020-12-05 06:12

    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;
    }    
    

提交回复
热议问题