user5787503
user5787503

Reputation:

Display emoji from unicode in Android

I have this simple code to show the hearts card suit in a button's text:

int intHearts= 0x2665;
String stringHearts= new String(Character.toChars(intHearts));
btnHearts.setText(stringHearts);

and it work fine in the emulator (Android 7), displaying the red emoji. Instead, when I run the application from my Asus Zenfone 2 (Android 5), I can only see the black character of hearts ♥. So how do I get my phone to display the red emoji?

Upvotes: 0

Views: 2112

Answers (2)

letsCode
letsCode

Reputation: 3044

Set a unicode.

int smileEmoji = 0x1F60A;

where you want it to show.

getEmojiByUnicode(smileEmoji)

the method.

public String getEmojiByUnicode(int unicode){
    return new String(Character.toChars(unicode));
}

Upvotes: 2

cketti
cketti

Reputation: 1377

U+2665 was added to Unicode long before emojis were introduced. That's why there are two presentation modes for this character (text and emoji). Which character presentation is used by default is somewhat app specific. For legacy reasons often the text presentation (black heart) is used.

You can manually specify the character presentation by appending a variation selector character. U+FE0E for text presentation. U+FE0F for emoji presentation.

Unfortunately, Android only seems to support the text presentation (ignoring variation selectors) before Android 6.0. And from then on only the emoji presentation (again, ignoring variation selectors).

To work around this you could use the EmojiCompat support library.

Upvotes: 2

Related Questions