I am not affiliated with or supported by Google or any other people who develop the EmojiCompat library. I only made a more flexible implementation of one of their classes. .
This is a library providing an easy solution to use EmojiCompat (or rather Emoji2
)
with different user-selectable emoji packs, including imported ones.
The library is available on Maven Central, Github Packages are not maintained anymore.
Integrating this library is done in a few steps.
- To use
Emoji2
without special widgets, you should useandroidx.appcompat
>=1.4.0
. This way, all AppCompat components are already EmojiCompat-enabled. - You need to include a dependency for the library in your app's
build.gradle
. It is split into four parts - you only need one of these:Package name Description de.c1710:filemojicompat:3.2.6
The main library part. It supports emoji fonts to be placed in your app's data
directory. Works like FilemojiCompat 2de.c1710:filemojicompat-ui:3.2.6
Includes an emoji picker and an emoji pack management. Can be integrated with androidx.preferences
. Recommendedde.c1710:filemojicompat-defaults:3.2.6
A default list of four downloadable emoji packs: Blobmoji, Noto-Emoji, Openmoji and Twemoji. Recommended if the app has internet permissions de.c1710:filemojicompat-autoinit:3.2.6
An extension for filemojicompat-defaults
which uses theandroidx.startup
library to directly initialize EmojiCompat with FilemojiCompat without having to modify yourApplication
class, similar to howEmoji2
would work. Recommended if you don't want to change the defaults or add any other emoji packs. - If you don't want to use the emoji picker, you can continue with the instructions for FilemojiCompat 2 below.
Otherwise, you need to initialize EmojiCompat/Emoji2/FilemojiCompat. If you usefilemojicompat-autoinit
, you can skip this step.
In your mainApplication
''sonCreate
function, the initialization can be done with one line (this example is for Kotlin, but it is the same for Java):override fun onCreate() { ... EmojiPackHelper.init(this, DefaultEmojiPackList.get(this)) }
DefaultEmojiPackList.get(this)
requiresfilemojicompat-defaults
.- If you want to use other emoji packs or extend the list, you can create an
EmojiPackList
instance and add the packs to it (or append toDefaultEmojiPackList.get(this)
). - If you want to disable/hide the file import feature, you can pass
allowPackImports = false
. For maximum flexibility for your users, it is recommended to leave it enabled. With this, your app should work, although it is not yet possible to change the emoji pack.
- Now, the emoji pack picker has to be integrated. There are two ways here:
- If you use the
androidx.preference
library, you can integrate it as a preference - If you don't use it or don't have any other preferences, you can also integrate it manually
- If you use the
Right now it is not possible to use it in XML.
Integrating the emoji picker with your preferences involves two steps.
- You need to add the
EmojiPickerPreference
to yourPreferenceFragment
(in code). This is done inonCreatePreferences()
. The example is based on https://developer.android.com/guide/topics/ui/settings#inflate_the_hierarchy:Note: The EmojiPickerPreference has to be created before the Activity is in aoverride fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResources(R.xml.preferences, rootKey) // Add the emoji preference getPreferenceScreen().addPreference(EmojiPickerPreference.get(requireActivity(), null)) }
resume
d state. - In order to make the picker dialog show up, you need to override
onDisplayPreferenceDialog()
:@Override public void onDisplayPreferenceDialog(@NonNull Preference preference) { if (!EmojiPickerPreference.onDisplayPreferenceDialog(this, preference)) { super.onDisplayPreferenceDialog(preference); } }
The actual picker is a special RecyclerView
adapter called EmojiPackItemAdapter
.
It can therefore be easily integrated with any RecyclerView.
The easiest way to get an instance is EmojiPackItemAdapter.get()
, which sets up everything to handle the emoji pack import.
However, it requires a Activity that supports the Android Activity Results API as a parameter, an androidx
ComponentActivity
fulfills these requirements.
That's relatively easy: Just add the following line to your module's >
build.gradle
insidedependencies
:implementation 'de.c1710:filemojicompat:3.2.2'
This version has rather changes in its API. Instead of using the constructor (
new FileEmojiCompatConfig(...)
), > you'll need to use theinit
-function, which should provide the same parameters.The
setReplaceAll(boolean, boolean)
-method is also not present anymore. It is replaced bysetReplaceAll(ReplaceStrategy)
. The following table translates the old parameters to the new one:
replaceAll
replaceAllonFallback
strategy
false
false
NEVER
true
false
NORMAL
(default)true
true
ALWAYS
false
true
Removed as it does not make much sense If you want, you can now add the
patchImeSupport
-function to add support for keyboards to show all the new emojis.
- First of all, you will need to create a configuration through the
init
-function. Examples:// All examples assume that you have an Android Context provided // Creates a configuration that only provides the option to use a custom emoji font FileEmojiCompatConfig config = FileEmojiCompatConfig.init(context); // Specifies that you want to use the file "Blobmoji.ttf" from the src/main/assets directory FileEmojiCompatConfig config = FileEmojiCompatConfig.init(context, null, "Blobmoji.ttf"); // Loads the font from /storage/emulated/0/Android/data/[your.app.package]/files/Blobmoji.ttf (if provided; otherwise a default/fallback is used) File fontFile = new File(context.getExternalFilesDir(null), "emoji/Blobmoji.ttf"); FileEmojiCompatConfig config = FileEmojiCompatConfig.init(context, fontFile);- Then you can add some additional configuration, e.g. that you want to replace all emojis, even if the fallback/default font is used:
// Just using the second example here. You can also call this function separately. FileEmojiCompatConfig config = FileEmojiCompatConfig.init(context, null, "Blobmoji.ttf") .setReplaceAll(ReplaceStrategy.ALWAYS);- Once you have your configuration, you'll need to initialize
EmojiCompat
with it:EmojiCompat.init(config);- Wherever you want to use it, you'll need to either use the EmojiCompat widgets or add it manually, as described on the website.
- For some reason, it does not seem to automatically add the metadata required for keyboards to show newer emojis. To do that you can simply call the following function with your text fields:
// Assuming that you have called the text input "editText" // When you still have the config Utils.patchImeSupport(editText, config); // When e.g. you have your replacement strategy set to ALWAYS and therefore can be sure that all // emojis are replaced or if you just don't care if something is a bit odd // (I am not sure what exactly this does on the keyboard/IME side). Utils.patchImeSupport(editText, true);In this case, there won't be a visible difference to not using EmojiCompat.
As JCenter/Bintray will be shut down on 2021-05-01, the library needed to switch to another service, which is in this case MavenCentral (and Github Packages, but due to an issue, downloading through the maven repository is not possible without authentication).
While the usage of MavenCentral should be very easy (you only might need to include it in your
repositories
-section), there's one disadvantage: Older versions of the library (that is1.0.17
or older) will not be available anymore, although I don't believe that these are needed