I want to draw a single unicode character to a canvas, using drawText()
.
canvas.drawText("\u270c\ufe0e", x, y, paint);
On a test device running Android 7, it displays correctly:
But in my emulator, "running" Android 6, and on a real device running Android 6, it gets drawn as Emoji, regardless of the \ufe0e
:
This is of course not what I want, since I want to have it drawn black, not pink! Is there any way to "switch off" Emojis when drawing text?
You can try using the EmojiCompat library, described here and here. Add it to your dependencies;
dependencies {
...
implementation "com.android.support:support-emoji:27.1.1"
implementation "com.android.support:support-emoji-bundled:27.1.1"
...
}
Initialize the library;
EmojiCompat.init(new BundledEmojiCompatConfig(this).setReplaceAll(true));
And then replace TextView
with EmojiTextView
. Here's an example you can use to test:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="HardcodedText">
<!-- replace -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="TextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
<!-- with -->
<android.support.text.emoji.widget.EmojiTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="EmojiTextView - default: \u270c, text: \u270c\ufe0e, emoji: \u270c\ufe0f"/>
</LinearLayout>
If it doesn't work how you would like, remove .setReplaceAll(true)
from the initialization line, try again, and see if that works.
EDIT:
If you want to draw the emoji text manually e.g. to a canvas, you can do it with EmojiCompat.process(...)
and android.text.StaticLayout
. I haven't tried it so there might be errors, but it should work.
// assuming x, y, and paint are defined
CharSequence emoji = EmojiCompat.get().process("\u270c\ufe0e");
StaticLayout layout = new StaticLayout(emoji, paint,
canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
canvas.translate(x, y);
layout.draw(canvas);
canvas.translate(-x, -y);
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