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