How to Use Unicode in Android Resource

How to use unicode in Android resource?

Your character (U+1F4E1) is outside of Unicode BMP (Basic Multilingual Plane - range from U+0000 to U+FFFF).

Unfortunately, Android has very weak (if any) support for non-BMP characters. UTF-8 representation for non-BMP characters requires 4 bytes (0xF0 0x9F 0x93 0xA1). But, Android UTF-8 parser only understands 3 bytes maximum (see it here and here).

It works for you when you use UTF-16 surrogate form representation of this character: "\uD83D\uDCE1". If you were able to encode each surrogate UTF-16 character in modified UTF-8 (aka CESU-8) - it would take 6 bytes total (3 bytes in UTF-8 for each member of surrogate pair), then it would be possible. But, Android does not support CESU-8 explicitly either.

So, your current solution - hard-coding this symbol in source code as surrogate UTF-16 pair seems easiest, at least until Android starts fully supporting non-BMP UTF-8.

UPDATE: this seems to be partially fixed in Android 6.0. This commit has been merged into Android 6, and permits presence of 4-byte UTF-8 characters in XML resources. Its not perfect solution - it will simply automatically convert 4-byte UTF-8 into appropriate surrogate pair. However, it allows to move them from your source code into XML resources. Unfortunately, you can't use this solution until your application can stop supporting any Android version except for 6.0 and later.

Android - When should I escape unicode in Strings.xml resource file?

I don't have a definitive authority to point to, but in my own personal experience the only reason I have found to use the \u notation is when the character is visually ambiguous. For example, I use \u2013 instead of an actual en-dash character "–" because the glyph is so visually similar to the standard hyphen "-". Another common scenario is using \u00a0 instead of an actual non-breaking space character.

As far as I am aware, there is no technical reason to not use unicode characters in your strings file, as long as the file itself is using a character encoding that supports them.

Note that Android Studio's lint feature will actually prompt you to use these special characters in certain scenarios:

Sample Image

Applying the suggested fix leaves you with an ellipsis character "…", not the encoding for one.

Unicode characters not displayed in TextView.setText

Unfortunately, you just can't do it that way from strings.xml AFAIK.

You're left doing one of two things.

  • Adding the Unicode character within java to the String in the XML file:

    String str = "\u00A9" + getContext().getString(R.string.your_string);
  • Entering the text as HTML in java:

    yourTextView.setText(Html.fromHtml("your chars"));

Hope this is useful.

How to write character & in android strings.xml

Encode it:

&

Displaying the unicode characters with real devices and emulators

I think it's a font issue. The font used by Android studio may not supports that character while the android device, you will need to embed a font which support that character.

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\u270c"
android:textSize="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

This code worked for me and below one was the result in emulator PIXEL 5 API 32
Sample Image

Also, it seems to be device specific. Troubles with Unicode string encoding in Android



Related Topics



Leave a reply



Submit