Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect emoji support on Android by code

By code, I can make a button that inserts these 3 emojis into the text: ⚽️😈🐺

On many phones when the user clicks the button, though, the problem is that ⚽️😈🐺 displays as [X][X][X]. Or even worse, it displays only three empty spaces.

I would like to disable and hide my own built-in emoji-keypad on Android devices that do not display emojis correctly. Does anyone knows or have a tip on how to detect in code if a device has emoji support?

I have read that emoji is supported from android 4.1, but that is not my experience....

like image 833
Vidar Vestnes Avatar asked Aug 28 '16 18:08

Vidar Vestnes


People also ask

How do I see unsupported emojis?

If your device doesn't support emojis, you can still get them by using a third-party social messaging app such as WhatsApp or Line. However, you will only be able to see emojis inside these apps; any SMS messages you receive will continue not to display them.

How do I get Kaomojis on my Android?

You'll want to go to Settings > General, then scroll down and tap on Keyboard. Below a handful of toggle settings like Auto-Capitalization is the Keyboards setting. Tap that, then tap "Add New Keyboard." There, sandwiched between non-English language keyboards is the Emoji keyboard. Select it.


3 Answers

Based on Jason Gore answer:

For example create boolean canShowFlagEmoji:

 private static boolean canShowFlagEmoji() {
        Paint paint = new Paint();
        String switzerland = "\uD83C\uDDE8\uD83C\uDDED"; //  Here enter Surrogates of Emoji
        try {
            return paint.hasGlyph(switzerland);
        } catch (NoSuchMethodError e) {
            // Compare display width of single-codepoint emoji to width of flag emoji to determine
            // whether flag is rendered as single glyph or two adjacent regional indicator symbols.
            float flagWidth = paint.measureText(switzerland);
            float standardWidth = paint.measureText("\uD83D\uDC27"); //  U+1F427 Penguin
            return flagWidth < standardWidth * 1.25;
            // This assumes that a valid glyph for the flag emoji must be less than 1.25 times
            // the width of the penguin.
        }
    }

And then in code whenever when you need to check if emoji is available:

if (canShowFlagEmoji()){
    // Code when FlagEmoji is available
} else {
    // And when not
}

Surrogates of emoji you can get here, when you click on detail.

like image 195
Samuel Tulach Avatar answered Oct 11 '22 08:10

Samuel Tulach


An alternative option might be to include the Android "Emoji Compatibility" library, which would detect and add any required Emoji characters to apps running on Android 4.4 (API 19) and later: https://developer.android.com/topic/libraries/support-library/preview/emoji-compat.html

like image 20
Stephen Bridgett Avatar answered Oct 11 '22 10:10

Stephen Bridgett


I just implemented a solution for this problem myself. The nice thing with Android is that it is open source so that when you come around problems like these, there's a good chance you can find an approach to help you.

In the Android Open Source Project, you can find a method where they use Paint.hasGlyph to detect whether a font exists for a given emoji. However, as this method is not available before API 23, they also do test renders and compare the result against the width of 'tofu' (the [x] character you mention in your post.)

There are some other failings with this approach, but it should be enough to get you started.

Google source:

  • https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java#441
  • https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
like image 11
Jason Gore Avatar answered Oct 11 '22 10:10

Jason Gore