3

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

suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • Use a dedicated FREE font with all the glyphs. – Phantômaxx Aug 12 '14 at 10:23
  • 1
    I'm facing two problems: First, we haven't found a FREE emoji reource yet(but we're trying). Second, we still want to do this check(if possible) for some other reasons. Thanks – suitianshi Aug 12 '14 at 10:33
  • I can't see know **how** can you determine if a glyph is shown correctly or as a square? for Android it's the very same! there's no error code in return. So, really, use a FREE font which supports emojis (there are a TON). You can even consider to take the font out of another (working) device and use that one. – Phantômaxx Aug 12 '14 at 10:36
  • I will try. Anyway, I have to find a solution/workaround – suitianshi Aug 12 '14 at 10:39
  • you're right, but we will use emoji for business reasons, we must make sure that's allowed by its license. See http://stackoverflow.com/questions/10834811/emoji-copyright/16696512#16696512 – suitianshi Aug 12 '14 at 11:03
  • Well, you said you have some devices (presumably 4.x) where the app runs fine. Take out the system font from that device and use it as a custom font for your app... (you'll need to root the "donor" device). If it is a system font, in Android, everything is open source / free. That's Google's power! – Phantômaxx Aug 12 '14 at 11:09
  • Thanks your advice. I will have try. – suitianshi Aug 13 '14 at 02:54
  • As a **last resource**, you might use some SVG graphics (which is vectorial, as TTF fonts) - You will need a free 3rd party library. But then you'll have to save it as PNGs (if not existing) into your SD card and insert it in text as `` and use HTML.fomHTML. Quite an overkill. – Phantômaxx Aug 13 '14 at 07:28
  • I know this is an old thread but just in case. Just because you have a license to use a font on a device, it doesn't mean you are allowed to redistribute the font with your app. @suitianshi is right in wanting to check the font permissions before redistributing. – guioconnor Apr 12 '18 at 10:01

3 Answers3

6

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.

RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • 1
    this is a great solution, because no other two methods suit: both flag/penguin and getPixels comparison fail for SDK10 – djdance Feb 26 '17 at 14:54
1

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)

bapho
  • 858
  • 8
  • 13
0

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.
    }
}