How to set emoji by unicode in a textview?

89,876

Solution 1

Found a solution:

In my unicode I replaced 'U+' by '0x'

Example: replace 'U+1F60A' by '0x1F60A'

This way I got an 'int' like

int unicode = 0x1F60A;

Which can be used with

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

So Textview displays 😊 without Drawable

Try it with http://apps.timwhitlock.info/emoji/tables/unicode

Solution 2

You can directly use Emojis in string resources by using the decimal code like this:

😊

for example:

<string name="emoji">I am happy &#128522;</>

Solution 3

Note: For Kotlin

fun getEmoji(unicode: Int): String {
    return String(Character.toChars(unicode))
}

Solution 4

all of the credit to Kenneth Murerwa, whose answer solved my problem. just chiming in that just copy and paste what you get from the 'copy' button at https://emojipedia.org between good old quotation marks. Yeah, it's a noob point but hey, we're all noobs at the beggining 😂

val emoji = "\uD83D\uDE00 \uD83D\uDC4C"

and then you can add it to whatever string you need. It renders on the phone screen fine, though it won't show up in a println

println("👌")

Solution 5

I think I found the most straightforward solution. In my case, I wanted to add a fire (🔥) emoji to one of the chips in a chip group. I simply went to the Emojipedia Fire Entry1, clicked on the copy button just below the emoji meaning, and literally just pasted it into my Kotlin code. Here is a code snippet of how it looked like after pasting.

val chip = Chip(context)
chip.text = "\uD83D\uDD25 New"

Here is how the code looks like once I run it on my device. I included the other chips as well 😉;

An image sample of how the chip looks like in the device

PS: I ran this on the latest version of Android Studio (Arctic Fox v. 2020.3.1). Results may differ with older versions.

Footnote

  1. Emojipedia is a free encyclopedia that lists and provides meanings for all the emojis approved under the Unicode standard. You can always head out there for insightful emoji meanings and for other emoji needs.
Share:
89,876

Related videos on Youtube

Gilbert Giesbert
Author by

Gilbert Giesbert

Updated on March 21, 2022

Comments

  • Gilbert Giesbert
    Gilbert Giesbert over 2 years

    Hi I'd like to do the following:

    ??? unicode = U+1F60A
    String emoji = getEmojiByUnicode(unicode)
    String text = "So happy "
    textview.setText(text + emoji);
    

    to get this in my textview:

    So happy 😊

    How can I implement getEmojiByUnicode(unicode)?

    What type should the unicode variable be? (String, char, int?)

    Please note that I do NOT want to use Drawables!

  • filthy_wizard
    filthy_wizard almost 8 years
    works on 5.1 but on 4.4.4 i get a ? for my emoji string
  • Pwnstar
    Pwnstar almost 8 years
    Is it possible to do this in strings.xml file ?
  • Suragch
    Suragch over 7 years
    @user1232726, This will depend on the user's phone including the emoji being used.
  • Ravindra Kushwaha
    Ravindra Kushwaha almost 7 years
    Can u please help to convert this "5794d5f7895fa10a8f8e1357" into the EMOJI.. @GilbertGiesbert ..Thanks...
  • Daksh Gargas
    Daksh Gargas over 6 years
    What if I have the unicode in the form of string? How do I convert it into integer? Parsing makes in invalid as it removes 'F' from it.
  • Chris - Jr
    Chris - Jr over 6 years
    Unfortunately it doesn't work with 32 bit Unicode, like U+1F1EF U+1F1F2. This value is outside the integer boundaries.
  • Adinia
    Adinia about 6 years
    This is a good answer; to find the decimal code of an emoji, you can use something like quackit.com/character_sets/emoji
  • Bipin Bharti
    Bipin Bharti almost 6 years
    NOte: For Kotlin > fun getEmoji(unicode: Int): String { return String(Character.toChars(unicode)) }
  • hmac
    hmac over 5 years
    i don't find the x necessary
  • Dr. aNdRO
    Dr. aNdRO over 5 years
    Error while build: Character reference "&#
  • Vadim Kotov
    Vadim Kotov about 5 years
    @hmac I've rolled back the answer, removed mentions of x