How to set emoji by unicode in a textview?
AndroidUnicodeTextviewEncodeEmojiAndroid Problem Overview
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!
Android Solutions
Solution 1 - Android
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 - Android
You can directly use Emojis in string resources by using the decimal code like this:
😊
for example:
<string name="emoji">I am happy 😊</>
Solution 3 - Android
Note: For Kotlin
fun getEmoji(unicode: Int): String {
return String(Character.toChars(unicode))
}
Solution 4 - Android
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 - Android
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 ;
PS: I ran this on the latest version of Android Studio (Arctic Fox v. 2020.3.1). Results may differ with older versions.
Footnote
- 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.
Solution 6 - Android
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
Solution 7 - Android
// 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 - "📁"
// 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 ""
}