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