suitianshi
suitianshi

Reputation: 3340

how can I check if my device is capable to render Emoji images correctly?

I'm using Emoji unicode in a View. On most devices the images appear ok, but on one of my low-end device(android 2.3) they are rendered as little squares.

Can I check whether the device support emoji? So that I can publish my apk while won't show that ugly squares on some devices.

enter image description here

Upvotes: 3

Views: 1633

Answers (3)

Rahul Verma
Rahul Verma

Reputation: 1

https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java#441

Inspired from the two methods found in the above file.

   public static boolean canShowEmoji(String emoji) {
    Paint paint = new Paint();
    float tofuWidth = paint.measureText("\uFFFE");
    float standardWidth = paint.measureText("\uD83D\uDC27");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return paint.hasGlyph(emoji);
    } else {
        float emojiWidth = paint.measureText(emoji);
        return emojiWidth > tofuWidth && emojiWidth < standardWidth * 1.25;
        // This assumes that a valid glyph for the cheese wedge must be greater than the width
        // of the noncharacter.
    }
}

Upvotes: 0

RogueBaneling
RogueBaneling

Reputation: 4471

This is a late answer but I recently ran into a similar problem. I needed to filter through a List<String> and filter out emojis that couldn't be rendered on the device (i.e., if the device was old and didn't support rendering them).

What I ended up doing was using Paint to measure the text width.

Paint mPaint = new Paint();
private boolean emojiRenderable(String emoji) {
    float width = mPaint.measureText(emoji);
    if (width > 7) return true;
    return false;
}

The width > 7 part is particularly hacky, I would expect the value to be 0.0 for non-renderable emoji, but across a few devices, I found that the value actually ranged around 3.0 to 6.0 for non-renderable, and 12.0 to 15.0 for renderable. Your results may vary so you might want to test that. I believe the font size also has an effect on the output of measureText() so keep that in mind.

Overall I am not sure if this is a great solution but it's the best that I've come up with so far.

Upvotes: 6

bapho
bapho

Reputation: 936

please check the source code from Googles Mozc project. The EmojiRenderableChecker class seems to work pretty well! https://github.com/google/mozc/blob/master/src/android/src/com/google/android/inputmethod/japanese/emoji/EmojiRenderableChecker.java

it's like a compat version for Paint.hasGlypgh (added in Marshmallow). https://developer.android.com/reference/android/graphics/Paint.html#hasGlyph(java.lang.String)

Upvotes: 1

Related Questions