How to set emoji by unicode in a textview?
Asked Answered
M

8

114

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!

Mushro answered 12/11, 2014 at 18:19 Comment(0)
M
212

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

Mushro answered 12/11, 2014 at 18:40 Comment(7)
works on 5.1 but on 4.4.4 i get a ? for my emoji string – Handmade
Is it possible to do this in strings.xml file ? – Hera
@user1232726, This will depend on the user's phone including the emoji being used. – Making
Can u please help to convert this "5794d5f7895fa10a8f8e1357" into the EMOJI.. @GilbertGiesbert ..Thanks... – Kunz
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. – Toed
Unfortunately it doesn't work with 32 bit Unicode, like U+1F1EF U+1F1F2. This value is outside the integer boundaries. – Calends
NOte: For Kotlin > fun getEmoji(unicode: Int): String { return String(Character.toChars(unicode)) } – Ingmar
P
63

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

😊

for example:

<string name="emoji">I am happy &#128522;</>
Pond answered 25/5, 2018 at 8:21 Comment(4)
This is a good answer; to find the decimal code of an emoji, you can use something like quackit.com/character_sets/emoji – Allhallowtide
i don't find the x necessary – Vacillation
Error while build: Character reference "&# – Hypoplasia
@Vacillation I've rolled back the answer, removed mentions of x – Shakeup
I
7

Note: For Kotlin

fun getEmoji(unicode: Int): String {
    return String(Character.toChars(unicode))
}
Ingmar answered 18/7, 2018 at 13:10 Comment(0)
R
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.
Recollect answered 5/8, 2021 at 6:33 Comment(0)
C
5

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("πŸ‘Œ")
Chick answered 1/9, 2021 at 1:34 Comment(0)
I
1

You can do as below:

Unicode : uni-1F4A1

FYI, I am using Kotlin.

Create utility function as below:

private fun getEmojiByUnicode(reactionCode: String): String {
        val code = reactionCode.substring(4).toInt(16)
        return String(Character.toChars(code))
}

Where substring(4) will be discarded uni- these 3 characters and you have 1F4A1.

Set Emoji into TextView: (I am using ViewBinding in my Project)

mViewBinding.textViewEmoji.text = getEmojiByUnicode(data.Reaction)

For more details: Integer.parseInt ("0x1F60A") ends with NumberformatException

Indonesia answered 1/9, 2021 at 12:5 Comment(0)
Z
0
// example of unicode emoji - "U+1F4C1"
// other formats will return empty string
fun unicodeEmojiToHtmlEmoji(emoji: String): CharSequence {
    val inEmojiPrefix = "U+"
    val outEmojiPrefix = "&#x"
    val outEmojiSuffix = ";"
    return try {
        HtmlCompat.fromHtml(
            emoji.replace(
                inEmojiPrefix,
                outEmojiPrefix, true) + outEmojiSuffix,
            HtmlCompat.FROM_HTML_MODE_LEGACY
        )
    } catch (e: Throwable) {
        ""
    }
}

// example of html emoji - "&#x1F4C1;"
// other formats will return empty string
fun htmlEmojiToUnicodeEmoji(emoji: String): CharSequence {
    val outEmojiPrefix = "U+"
    return if(emoji.isNotBlank()) outEmojiPrefix + emoji.codePointAt(0).let(Integer::toHexString) else ""
}
Zestful answered 21/3, 2022 at 8:39 Comment(0)
E
0

For me, emoji Unicodes didn't work. So I just copied them and pasted them into my XML string and everything worked fine!

Earldom answered 14/2, 2023 at 13:21 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.