Android Emoji Compatibility
There is a support library introduced in Android to support the emoji backward compatibility which is EmojiCompat. It prevents the app from showing missing emoji characters in the form of ☐, which indicates that the device does not have a font to display the text.
How to configure this library
With Downloadable fonts configuration, the emoji fonts are downloaded at run time on first request, if they do not exist on the device. To use the downloadable fonts configuration:
a) First you need to add the following support library in the build.gradle file of application
compile "com.android.support:support-emoji:$version"
b) Now init the EmojiCompat with the font request inside your Application class
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
val fontRequest = FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
"Noto Color Emoji Compat", R.array.com_google_android_gms_fonts_certs)
val config = FontRequestEmojiCompatConfig(this, fontRequest)
// other config settings......
EmojiCompat.init(config)
}
}
With bundled fonts configuration, your app already has the package that includes the font with the embedded metadata. To use the bundled fonts configuration:
a) First you need to add the following support library in the build.gradle file of application
compile "com.android.support:support-emoji-bundled:$version"
b) Initialise the EmojiCompat using bundled configuration
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
val config = BundledEmojiCompatConfig(this)
EmojiCompat.init(config)
}
}
Now the Application is ready to use the EmojiCompat library.
How to use this library
- With Widgets
a) you need to replace your EditText, TextView, Button with the following widgets
EditText -> android.support.text.emoji.widget.EmojiEditText
TextView -> android.support.text.emoji.widget.EmojiTextView
Button -> android.support.text.emoji.widget.EmojiButton
b) if you are using AppCompat like AppCompatEditText, AppCompatTextView, AppCompatButton then you need to add the following EmojiCompat library to support AppCompat
compile "com.android.support:support-emoji-appcompat:$version"
and now replace your widgets to
AppCompatEditText -> android.support.text.emoji.widget.EmojiAppCompatEditText
AppCompatTextView -> android.support.text.emoji.widget.EmojiAppCompatTextView
AppCompatButton -> android.support.text.emoji.widget.EmojiAppCompatButton
- Without Widgets
val processed = EmojiCompat.get().process("neutral face \uD83D\uDE10");
now pass the processed CharSequence to your View.
For more info you can read the official documentation at the following link