Prevent character being drawn as emoji Prevent character being drawn as emoji android android

Prevent character being drawn as emoji


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 definedCharSequence 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);


You can try Fontawesome. You won't need to download the whole library. Just the ttf file from the website. It's around 18MB.

https://fontawesome.com/get-started

Then you can use the strings in the cheat sheet to draw icons.

https://fontawesome.com/cheatsheet

Copy the ttf file to your assets folder.

Typeface typeface = Typeface.createFromAsset(context.getAssets(),"fontawesome_webfont_4.ttf");paint.setTypeface(typeface);canvas.drawText("\uf25b", x, y, paint);

You can find \uf25b in the cheatsheet as f25b. It's the peace icon.