With the EmojiCompat library(part of the Support Library 26) your app can get backward-compatible emoji support on devices with API level 19+ and get rid of ☐ (tofu).
How does EmojiCompat work?
For a given char sequence, EmojiCompat can identify the emojis, replace them with the EmojiSpan, and then render the glyphs.On versions prior to API level 19, you’ll still get the tofu characters. EmojiCompat build on the new font mechanism to make sure you always have the latest emoji available.
Downloadable fonts configuration of emoji
The downloadable fonts configuration uses the Downloadable Fonts support library feature to download an emoji font. It also updates the necessary emoji metadata that the EmojiCompat support library needs to keep up with the latest versions of the Unicode specification.
Adding support library dependency
Support Library 26 has now been moved to Google’s maven repository, first include that in your project level build.gradle.
buildscript { repositories { google() } .... }
Add the support library in your app level build.gradle.
dependencies { ... compile "com.android.support:support-emoji:26.0.2" }
Adding certificates
When a font provider is not preinstalled or if you are using the support library, you must declare the certificates the font provider is signed with. The system uses the certificates to verify the font provider’s identity. Create a string array with the certificate details.
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="certs"> <item>MIIEqDCCA5CgAwIBAgIJA071MA0GCSqGSIb3DQEBBAUAMIGUMQsww...</item> </string-array> </resources>
Initializing the downloadable font configuration
Before using EmojiCompat, the library needs a one-time asynchronous setup(in application class).
//onCreate FontRequest fontRequest = new FontRequest( "com.google.android.gms.fonts", "com.google.android.gms", "Noto Color Emoji Compat", R.array.com_google_android_gms_fonts_certs);
When a downloadable font configuration, create your FontRequest
, and the FontRequestEmojiCompatConfig
object.
config = new FontRequestEmojiCompatConfig(getApplicationContext(), fontRequest) .setReplaceAll(true) .registerInitCallback(new EmojiCompat.InitCallback() { @Override public void onInitialized() { super.onInitialized(); Log.i(TAG, "Emojicompact Initialize"); } @Override public void onFailed(@Nullable Throwable throwable) { super.onFailed(throwable); Log.e(TAG, "Emojicompact Initialize failed " + throwable); } }); EmojiCompat.init(config);
It depends on the way you’re using it, the initialization takes at least 150 ms, even up to a few seconds. So you might want to get notified about its state.for this, use the registerInitCallback
method. Use EmojiCompat widgets in layout XMLs. If you are using AppCompat, refer to the Using EmojiCompat widgets with AppCompat section.
<android.support.text.emoji.widget.EmojiTextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> <android.support.text.emoji.widget.EmojiEditText android:layout_width="wrap_content" android:layout_height="wrap_content"/> <android.support.text.emoji.widget.EmojiButton android:layout_width="wrap_content" android:layout_height="wrap_content"/>
You can preprocess a char Sequence using the process method. You can then reuse the result instead of the initial registering in any widget that can render spanned instances.So, for example, if you’re doing your own custom drawing, you can use this to display emoji text.
EmojiCompat.get().registerInitCallback(new EmojiCompat.InitCallback() { @Override public void onInitialized() { final EmojiCompat compat = EmojiCompat.get(); normalTextView.setText( compat.process("Normal TextView \uD83D\uDE48\uD83D\uDE49\uD83D\uDE4A")); } });