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