I'm using the EmojiCompat support library to have emoji in my app, and I would like to have those emojis directly in the strings.xml
file (ex: <string name="string_name">Some text \uD83E\uDD96</string>
)
This works well for devices API>23 but otherwise it crash
A/art: art/runtime/check_jni.cc:65] JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0
A/art: art/runtime/check_jni.cc:65] string: 'Some text 🦖'
A/art: art/runtime/check_jni.cc:65] in call to NewStringUTF
A/art: art/runtime/check_jni.cc:65] from java.lang.String android.content.res.StringBlock.nativeGetString(long, int)
The only solution I have found is to get the emoji code in a java variable and put it at the end of my string (like the google sample) and it works on every version supported.
But I really want to put emoji code in the strings files and be able to do something like this :
emojiTextView.setText(getText(R.string.string_name));
Does anyone know how to achieve this ?
Its bit late to answer this question but still it may help you or someone else.
Use decimal
value you emoji to add an emoji in strings.xml
. This works well with Support Library as well.
In your case you want to display 🦖, it is T-REX (check here) and its decimal value is 🦖
. Final string will be:
<string name="emoji">Some text 🦖</string>
Output:
I faced this exact same issue in my app, I am not qualified to say how much this can limit your use of emojis in android in general, but I can say for certain that you can't add emojis to android xml files (without making your app crash, that is).
BUT, you can do a little hack to achieve this. Use This java static list of all emojis. In this you need to give the emoji name (emojiID) like "DOG_FACE" or "HONEY_BEE".
In your strings.xml file add these EmojiIds, but with in a tag form like: "##DOG_FACE##" or "##HONEY_BEE##" (For example : "Hi! how are you my man ##DOG_FACE##").
And make a method which takes the string's resource id as parameter and return the emoji contained string. This method will look for a regex pattern ^##.*##$
. You can name your method like:-
emojiTextView.setText(getTextWithEmojis(R.string.string_name));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With