Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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-fun

Second, I found how to display emoji icon at here: https://github.com/sharakova/EmojiTextView/blob/master/src/jp/sharakova/android/emoji/EmojiTextView.java

The EmojiTextView.java can convert the predefined character to emoji icon automatically. Therefore, I want to replace all the occurrences of emoji icon in a String to some predefined character, and put the result to EmojiTextView.java The problem is my code cannot recognize emoji icon in the String which contains emoji icon.

Here is my code snippet - I am trying to find if the input match any unicode of emoji icon:

// Array list of all emoji icon
private static final String[] ArrayEUnicodeString ={
    "uE415",
    "uE056",
    "uE057",
    ...
}

// Nothing matched when it receive emoji icon with unicode "uE415" from iphone. 'input' is message received from XMPP server
for (int i=0; i < emojiLength; i++)
{

    if (input.getBytes() == ArrayEUnicodeString[i].getBytes())
        Log.e("test", "ArrayEUnicodeString found");
}
// Note: iphone can display the emoji icon if I send "uE415"

I am not good at unicode comparison/convention. Can somebody help me please, thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
304 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here, Please go through below solution :

Problem : Inside TextView instead of Emoji, String ue415ue056ue057 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 ue415ue056ue057 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;
}    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...