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 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 69Pre-requisites 70-------------- 71 72- Android SDK 27 73- Android Build Tools v27.0.2 74- Android Support Repository 75 76Screenshots 77------------- 78 79<img src="screenshots/screenshot-1.png" height="400" alt="Screenshot"/> 80 81Getting Started 82--------------- 83 84This sample uses the Gradle build system. To build this project, use the 85"gradlew build" command or use "Import Project" in Android Studio. 86 87Support 88------- 89 90- Google+ Community: https://plus.google.com/communities/105153134372062985968 91- Stack Overflow: http://stackoverflow.com/questions/tagged/android 92 93If you've found an error in this sample, please file an issue: 94https://github.com/googlesamples/android-DownloadableFonts 95 96Patches are encouraged, and may be submitted by forking this project and 97submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details. 98 99License 100------- 101 102Copyright 2017 The Android Open Source Project, Inc. 103 104Licensed to the Apache Software Foundation (ASF) under one or more contributor 105license agreements. See the NOTICE file distributed with this work for 106additional information regarding copyright ownership. The ASF licenses this 107file to you under the Apache License, Version 2.0 (the "License"); you may not 108use this file except in compliance with the License. You may obtain a copy of 109the License at 110 111http://www.apache.org/licenses/LICENSE-2.0 112 113Unless required by applicable law or agreed to in writing, software 114distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 115WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 116License for the specific language governing permissions and limitations under 117the License. 118