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