xref: /aosp_15_r20/developers/build/prebuilts/gradle/DownloadableFonts/kotlinApp/README.md (revision d353a188ca6ec4b5eba25b5fbd7bcb8ce61322fb)
1*d353a188SXin LiAndroid DownloadableFonts Sample (Kotlin)
2*d353a188SXin Li===================================
3*d353a188SXin Li
4*d353a188SXin LiThis sample demonstrates how to use the Downloadable Fonts feature introduced in Android O.
5*d353a188SXin LiDownloadable Fonts is a feature that allows apps to request a certain font from a provider
6*d353a188SXin Liinstead of bundling it or downloading it themselves. This means, there is no need to bundle the
7*d353a188SXin Lifont as an asset.
8*d353a188SXin Li
9*d353a188SXin LiIntroduction
10*d353a188SXin Li------------
11*d353a188SXin Li
12*d353a188SXin LiThere are two ways of requesting a font to download.
13*d353a188SXin LiTo request a font to download from Java code, you need to create a [FontRequest][1] class first like
14*d353a188SXin Lithis:
15*d353a188SXin Li```java
16*d353a188SXin LiFontRequest request = new FontRequest(
17*d353a188SXin Li    "com.google.android.gms.fonts", // ProviderAuthority
18*d353a188SXin Li    "com.google.android.gms",  // ProviderPackage
19*d353a188SXin Li    query,  // Query
20*d353a188SXin Li    R.array.com_google_android_gms_fonts_certs); // Certificates
21*d353a188SXin Li```
22*d353a188SXin LiThe parameters `ProviderAuthority`, `ProviderPackage` are given by a font provider, in the case
23*d353a188SXin Liabove uses Google Play Services as a font provider.
24*d353a188SXin LiThe third parameter is a query string about the requested font. The syntax of the query is defined
25*d353a188SXin Liby the font provider.
26*d353a188SXin Li
27*d353a188SXin LiThen pass the request instance to the `requestFont` method in the [FontsContractCompat][2].
28*d353a188SXin Li```java
29*d353a188SXin LiFontsContractCompat.requestFont(context, request, callback, handler);
30*d353a188SXin Li```
31*d353a188SXin LiThe downloaded font or an error code if the request failed will be passed to the callback.
32*d353a188SXin LiThe example above assumes you are using the classes from the support library. There are
33*d353a188SXin Licorresponding classes in the framework, but the feature is available back to API level 14 if you
34*d353a188SXin Liuse the support library.
35*d353a188SXin Li
36*d353a188SXin LiYou can declare a downloaded font in an XML file and let the system download it for you and use it
37*d353a188SXin Liin layouts.
38*d353a188SXin Li```xml
39*d353a188SXin Li<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
40*d353a188SXin Li        app:fontProviderAuthority="com.google.android.gms.fonts"
41*d353a188SXin Li        app:fontProviderPackage="com.google.android.gms"
42*d353a188SXin Li        app:fontProviderQuery="Lobster Two"
43*d353a188SXin Li        app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
44*d353a188SXin Li</font-family>
45*d353a188SXin Li```
46*d353a188SXin LiBy defining the requested font in an XML file and putting the `preloaded_fonts` array and the
47*d353a188SXin Limeta-data tag in the AndroidManifest, you can avoid the delay until the font is downloaded by the
48*d353a188SXin Lifirst attempt.
49*d353a188SXin Li```xml
50*d353a188SXin Li<resources>
51*d353a188SXin Li    <array name="preloaded_fonts" translatable="false">
52*d353a188SXin Li        <item>@font/lobster_two</item>
53*d353a188SXin Li    </array>
54*d353a188SXin Li</resources>
55*d353a188SXin Li```
56*d353a188SXin Li
57*d353a188SXin Li```xml
58*d353a188SXin Li<application >
59*d353a188SXin Li    ...
60*d353a188SXin Li    <meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts" />
61*d353a188SXin Li    ...
62*d353a188SXin Li</application>
63*d353a188SXin Li```
64*d353a188SXin Li
65*d353a188SXin LiNote that the sample uses Google Play Services as a font provider, which requires pre-released
66*d353a188SXin Liversion of Google Play Services.
67*d353a188SXin LiYou can sign up for the beta program so that the beta version of Google Play Services is
68*d353a188SXin Lidownloaded to your device. https://developers.google.com/android/guides/beta-program
69*d353a188SXin LiIf you have Google Play Services whose version number is equal or above 11.x.x, that means you
70*d353a188SXin Lihave the compatible version installed. (You can confirm by navigating to
71*d353a188SXin LiSettings -> Apps -> Google Play Services)
72*d353a188SXin Li
73*d353a188SXin Li[1]: https://developer.android.com/reference/android/support/v4/provider/FontRequest.html
74*d353a188SXin Li[2]: https://developer.android.com/reference/android/support/v4/provider/FontsContractCompat.html
75*d353a188SXin Li
76*d353a188SXin LiPre-requisites
77*d353a188SXin Li--------------
78*d353a188SXin Li
79*d353a188SXin Li- Android SDK 25
80*d353a188SXin Li- Android Build Tools v25.0.3
81*d353a188SXin Li- Android Support Repository
82*d353a188SXin Li
83*d353a188SXin LiScreenshots
84*d353a188SXin Li-------------
85*d353a188SXin Li
86*d353a188SXin Li<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/>
87*d353a188SXin Li
88*d353a188SXin LiGetting Started
89*d353a188SXin Li---------------
90*d353a188SXin Li
91*d353a188SXin LiThis sample uses the Gradle build system. To build this project, use the
92*d353a188SXin Li"gradlew build" command or use "Import Project" in Android Studio.
93*d353a188SXin Li
94*d353a188SXin LiSupport
95*d353a188SXin Li-------
96*d353a188SXin Li
97*d353a188SXin Li- Google+ Community: https://plus.google.com/communities/105153134372062985968
98*d353a188SXin Li- Stack Overflow: http://stackoverflow.com/questions/tagged/android
99*d353a188SXin Li
100*d353a188SXin LiIf you've found an error in this sample, please file an issue:
101*d353a188SXin Lihttps://github.com/googlesamples/android-DownloadableFonts
102*d353a188SXin Li
103*d353a188SXin LiPatches are encouraged, and may be submitted by forking this project and
104*d353a188SXin Lisubmitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
105*d353a188SXin Li
106*d353a188SXin LiLicense
107*d353a188SXin Li-------
108*d353a188SXin Li
109*d353a188SXin LiCopyright 2017 The Android Open Source Project, Inc.
110*d353a188SXin Li
111*d353a188SXin LiLicensed to the Apache Software Foundation (ASF) under one or more contributor
112*d353a188SXin Lilicense agreements.  See the NOTICE file distributed with this work for
113*d353a188SXin Liadditional information regarding copyright ownership.  The ASF licenses this
114*d353a188SXin Lifile to you under the Apache License, Version 2.0 (the "License"); you may not
115*d353a188SXin Liuse this file except in compliance with the License.  You may obtain a copy of
116*d353a188SXin Lithe License at
117*d353a188SXin Li
118*d353a188SXin Lihttp://www.apache.org/licenses/LICENSE-2.0
119*d353a188SXin Li
120*d353a188SXin LiUnless required by applicable law or agreed to in writing, software
121*d353a188SXin Lidistributed under the License is distributed on an "AS IS" BASIS, WITHOUT
122*d353a188SXin LiWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
123*d353a188SXin LiLicense for the specific language governing permissions and limitations under
124*d353a188SXin Lithe License.
125