xref: /aosp_15_r20/developers/build/prebuilts/gradle/EmojiCompat/kotlinApp/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin LiAndroid EmojiCompat Sample (Kotlin)
2*d353a188SXin Li===================================
3*d353a188SXin Li
4*d353a188SXin LiThis sample demonstrates usage of EmojiCompat support library. You can use this library
5*d353a188SXin Lito prevent your app from showing missing emoji characters in the form of tofu (□). You
6*d353a188SXin Lican use either bundled or downloadable emoji fonts. This sample shows both usages.
7*d353a188SXin Li
8*d353a188SXin LiIntroduction
9*d353a188SXin Li------------
10*d353a188SXin Li
11*d353a188SXin LiThe EmojiCompat support library aims to keep Android devices up to date with the latest emoji. It
12*d353a188SXin Liprevents your app from showing missing emoji characters in the form of ☐, which indicates that your
13*d353a188SXin Lidevice does not have a font to display the text. By using the EmojiCompat support library, your app
14*d353a188SXin Liusers do not need to wait for Android OS updates to get the latest emoji.
15*d353a188SXin Li
16*d353a188SXin LiFor further detail, read [Emoji Compatibility][1] documentation.
17*d353a188SXin Li
18*d353a188SXin Li### Configuration
19*d353a188SXin Li
20*d353a188SXin LiYou need to first initialize EmojiCompat to load the metadata and the typeface. You can use either
21*d353a188SXin Libundled or downloadable fonts.
22*d353a188SXin Li
23*d353a188SXin Li#### Use downloadable fonts
24*d353a188SXin Li
25*d353a188SXin Li***You need the beta version of Google Play Services to use this feature.*** Join
26*d353a188SXin Li[Google Play Services Public Beta Program][4] and make sure you have v11 installed on your device
27*d353a188SXin Lirunning Android O Developer Preview 2.
28*d353a188SXin Li
29*d353a188SXin LiFor the downloadable font configuration, you need to create an instance of the [FontRequest][5]
30*d353a188SXin Liclass, and provide the font provider authority, the font provider package, the font query, and a
31*d353a188SXin Lilist of set of hashes for the certificates. For more information about FontRequest, refer to the
32*d353a188SXin LiDownloadable Fonts documentation. You can then create an instance of
33*d353a188SXin Li[FontRequestEmojiCompatConfig][6] and pass it to EmojiCompat.init().
34*d353a188SXin Li
35*d353a188SXin Li```java
36*d353a188SXin Lifinal FontRequest fontRequest = new FontRequest(
37*d353a188SXin Li                    "com.google.android.gms.fonts",
38*d353a188SXin Li                    "com.google.android.gms",
39*d353a188SXin Li                    "Noto Color Emoji Compat",
40*d353a188SXin Li                    R.array.com_google_android_gms_fonts_certs);
41*d353a188SXin LiEmojiCompat.init(new FontRequestEmojiCompatConfig(getApplicationContext(), fontRequest)
42*d353a188SXin Li                    .setReplaceAll(true)
43*d353a188SXin Li                    .registerInitCallback(new EmojiCompat.InitCallback() {
44*d353a188SXin Li                        @Override
45*d353a188SXin Li                        public void onInitialized() {
46*d353a188SXin Li                            Log.i(TAG, "EmojiCompat initialized");
47*d353a188SXin Li                        }
48*d353a188SXin Li
49*d353a188SXin Li                        @Override
50*d353a188SXin Li                        public void onFailed(@Nullable Throwable throwable) {
51*d353a188SXin Li                            Log.e(TAG, "EmojiCompat initialization failed", throwable);
52*d353a188SXin Li                        }
53*d353a188SXin Li                    });)
54*d353a188SXin Li```
55*d353a188SXin Li
56*d353a188SXin Li#### Use bundled font
57*d353a188SXin Li
58*d353a188SXin LiIn order the use the bundled font, call init() method of [EmojiCompat][2] with an instance of
59*d353a188SXin Li[BundledEmojiCompatConfig][3].
60*d353a188SXin Li
61*d353a188SXin Li### Use EmojiCompat
62*d353a188SXin Li
63*d353a188SXin Li#### Built-in views
64*d353a188SXin Li
65*d353a188SXin LiThe easiest way to use EmojiCompat in your layout, is to use [EmojiAppCompatTextView][7],
66*d353a188SXin Li[EmojiAppCompatEditText][8], or [EmojiAppCompatButton][9]. You can use them in your layout XMLs or
67*d353a188SXin Licode. You can just set any text containing emoji and the widgets handle the rest.
68*d353a188SXin Li
69*d353a188SXin Li#### With regular TextViews
70*d353a188SXin Li
71*d353a188SXin LiIf you want to use EmojiCompat with a regular TextView, retrieve an instance of EmojiCompat by
72*d353a188SXin Licalling EmojiCompat.get() and call registerInitCallback method. You can pass an
73*d353a188SXin LiEmojiCompat.InitCallback and use the EmojiCompat#process() method there to transform emoji text into
74*d353a188SXin Lia backward-compatible format.
75*d353a188SXin Li
76*d353a188SXin Li#### With custom TextViews
77*d353a188SXin Li
78*d353a188SXin LiIf you want to use EmojiCompat in your custom TextView, you can create an instance of
79*d353a188SXin Li[EmojiTextViewHelper][10] and use it in some overridden methods, namely setFilters and setAllCaps.
80*d353a188SXin Li[CustomTextView.java][11] shows what to do inside them.
81*d353a188SXin Li
82*d353a188SXin Li[1]: https://developer.android.com/preview/features/emoji-compat.html
83*d353a188SXin Li[2]: https://developer.android.com/reference/android/support/text/emoji/EmojiCompat.html
84*d353a188SXin Li[3]: https://developer.android.com/reference/android/support/text/emoji/bundled/BundledEmojiCompatConfig.html
85*d353a188SXin Li[4]: https://developers.google.com/android/guides/beta-program
86*d353a188SXin Li[5]: https://developer.android.com/reference/android/support/v4/provider/FontRequest.html
87*d353a188SXin Li[6]: https://developer.android.com/reference/android/support/text/emoji/FontRequestEmojiCompatConfig.html
88*d353a188SXin Li[7]: https://developer.android.com/reference/android/support/text/emoji/widget/EmojiAppCompatTextView.html
89*d353a188SXin Li[8]: https://developer.android.com/reference/android/support/text/emoji/widget/EmojiAppCompatEditText.html
90*d353a188SXin Li[9]: https://developer.android.com/reference/android/support/text/emoji/widget/EmojiAppCompatButton.html
91*d353a188SXin Li[10]: https://developer.android.com/reference/android/support/text/emoji/widget/EmojiCompatViewHelper.html
92*d353a188SXin Li[11]: https://github.com/googlesamples/android-EmojiCompat/blog/master/app/src/main/java/com/example/android/emojicompat/CustomTextView.java
93*d353a188SXin Li
94*d353a188SXin LiPre-requisites
95*d353a188SXin Li--------------
96*d353a188SXin Li
97*d353a188SXin Li- Android SDK 25
98*d353a188SXin Li- Android Build Tools v25.0.3
99*d353a188SXin Li- Android Support Repository
100*d353a188SXin Li
101*d353a188SXin LiScreenshots
102*d353a188SXin Li-------------
103*d353a188SXin Li
104*d353a188SXin Li<img src="screenshots/1-main.png" height="400" alt="Screenshot"/>
105*d353a188SXin Li
106*d353a188SXin LiGetting Started
107*d353a188SXin Li---------------
108*d353a188SXin Li
109*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
110*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
111*d353a188SXin Li
112*d353a188SXin LiSupport
113*d353a188SXin Li-------
114*d353a188SXin Li
115*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
116*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
117*d353a188SXin Li
118*d353a188SXin LiIf you've found an error in this sample, please file an issue:
119*d353a188SXin Lihttps://github.com/googlesamples/android-EmojiCompat
120*d353a188SXin Li
121*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
122*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
123*d353a188SXin Li
124*d353a188SXin LiLicense
125*d353a188SXin Li-------
126*d353a188SXin Li
127*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
128*d353a188SXin Li
129*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
130*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
131*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
132*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
133*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
134*d353a188SXin Lithe License at
135*d353a188SXin Li
136*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
137*d353a188SXin Li
138*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
139*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
140*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
141*d353a188SXin LiLicense for the specific language governing permissions and limitations under
142*d353a188SXin Lithe License.
143