Skip to content

Instantly share code, notes, and snippets.

@prongbang
Created October 2, 2022 07:28
Show Gist options
  • Save prongbang/e89b9ef634d0ac33c9683305ee9213f1 to your computer and use it in GitHub Desktop.
Save prongbang/e89b9ef634d0ac33c9683305ee9213f1 to your computer and use it in GitHub Desktop.
Convert Unicode to Emoji for Android
import java.util.regex.Pattern
// How to use
//
// val actual = EmojiConverter.fromUnicode("เพชรพลอย U+1F48E 8920 เพชรพลอย U+1F48E 8920") // เพชรพลอย 💎 เพชรพลอย 💎 8920
// emojiTextView.setText(EmojiCompat.get().process(actual))
//
object EmojiConverter {
fun fromUnicode(textUnicode: String): String {
val pattern = Pattern.compile("(U\\+[A-F0-9]+)")
return try {
textUnicode.replace(pattern.toRegex()) { result ->
StringBuilder()
.appendCodePoint(Integer.decode(result.value.replace("U+", "0x")))
.toString()
}
} catch (e: Exception) {
textUnicode
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment