Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put utf-16 characters in Android string resource?

I want to use Emojis in my app's strings. All strings reside, of course, in strings.xml

The problem is that not all Emojis are 16 bit friendly. Some Emojis can be represented as "normal" 16 bit hex: '\u26FF' but some are 32 bit hexes (UTF-16), usually represented as: '\x1F600'. I have no problem dealing with those inside the app, in code. But the strings.xml resource file is UTF8 encoded, and does not deal properly with non 16 bit escape chars.

I tried using '\x1F600' - because I saw that '\u26FF' works just fine. But it seems not to devour the 'x' escape char. Nor did it like the regexp notation '\x{1F600}'

So I ended up using a string placeholder '%1$s' and filling in the Emoji in code like this:

// greeting_3 is defined as: "hello there %1$s!"
String s = context.getString(R.string.greeting_3, "😜");
// OR:
String s = context.getString(R.string.greeting_3, new String(Character.toChars(0x1F61C)));

This is not a very elegant solution... is there a proper way to put 32 bit UTF-8 chars in strings.xml ?

like image 607
Shoham Avatar asked Jul 20 '14 16:07

Shoham


People also ask

How to get the quantity of a string in Android?

The rules for deciding which case to use for a given language and quantity can be very complex, so Android provides you with methods such as getQuantityString () to select the appropriate resource for you. Although historically called "quantity strings" (and still called that in API), quantity strings should only be used for plurals.

How to escape special characters from a string in Android?

When a string contains characters that have special usage in XML, you must escape the characters according to the standard XML/HTML escaping rules. If you need to escape a character that has special meaning in Android you should use a preceding backslash. By default Android will collapse sequences of whitespace characters into a single space.

Where can I find more information about the Android text package?

You can check out the android.text package documentation on GitHub to learn more. For more information on working with spans, see the following links: You can apply complex or custom styling by using the Annotation class along with the <annotation> tag in your strings.xml resource files.

How do I hardcode a string in XML?

If you want to hardcode the string in xml directly, you can add it up this way. Show activity on this post. The simplest way I've found is to replace the '&' with the Unicode equivalent.


1 Answers

But the strings.xml resource file is UTF8

If it's UTF-8 encoded, you can put your emojis directly. But then you risk that your editor or another piece of software destroys them.

If you are putting them in XML, you can try using XML entities: &#x1f600;, I'm not sure how well Android supports them though.

You can also use surrogate pairs: convert the emoji to UTF-16 and use standard \u escape. You can for example check out this page, it even tells you how to create a string litaral in Java: http://www.fileformat.info/info/unicode/char/1F600/index.htm

😜 → U+1F600 → "\uD83D\uDE00"

like image 112
Karol S Avatar answered Oct 15 '22 16:10

Karol S