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 Note that the sample uses Google Play Services as a font provider, which requires pre-released 67 version of Google Play Services. 68 You can sign up for the beta program so that the beta version of Google Play Services is 69 downloaded to your device. https://developers.google.com/android/guides/beta-program 70 If you have Google Play Services whose version number is equal or above 11.x.x, that means you 71 have the compatible version installed. (You can confirm by navigating to 72 Settings -> Apps -> Google Play Services) 73 74 [1]: https://developer.android.com/reference/android/support/v4/provider/FontRequest.html 75 [2]: https://developer.android.com/reference/android/support/v4/provider/FontsContractCompat.html 76 77 Pre-requisites 78 -------------- 79 80 - Android SDK 25 81 - Android Build Tools v25.0.3 82 - Android Support Repository 83 84 Screenshots 85 ------------- 86 87 <img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> 88 89 Getting Started 90 --------------- 91 92 This sample uses the Gradle build system. To build this project, use the 93 "gradlew build" command or use "Import Project" in Android Studio. 94 95 Support 96 ------- 97 98 - Google+ Community: https://plus.google.com/communities/105153134372062985968 99 - Stack Overflow: http://stackoverflow.com/questions/tagged/android 100 101 If you've found an error in this sample, please file an issue: 102 https://github.com/googlesamples/android-DownloadableFonts 103 104 Patches are encouraged, and may be submitted by forking this project and 105 submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 106 107 License 108 ------- 109 110 Copyright 2017 The Android Open Source Project, Inc. 111 112 Licensed to the Apache Software Foundation (ASF) under one or more contributor 113 license agreements. See the NOTICE file distributed with this work for 114 additional information regarding copyright ownership. The ASF licenses this 115 file to you under the Apache License, Version 2.0 (the "License"); you may not 116 use this file except in compliance with the License. You may obtain a copy of 117 the License at 118 119 http://www.apache.org/licenses/LICENSE-2.0 120 121 Unless required by applicable law or agreed to in writing, software 122 distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 123 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 124 License for the specific language governing permissions and limitations under 125 the License. 126