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