1<?xml version="1.0" encoding="UTF-8"?> 2<!-- 3 Copyright 2017 The Android Open Source Project 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16--> 17<sample> 18 <name>DownloadableFonts</name> 19 <group>UI</group> 20 <package>com.example.android.downloadablefonts</package> 21 22 <strings> 23 <intro> 24<![CDATA[ 25This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 26Downloadable Fonts is a feature that allows apps to request a certain font from a provider 27instead of bundling it or downloading it themselves. This means, there is no need to bundle the 28font as an asset. 29]]> 30 </intro> 31 </strings> 32 33 <template src="base-build" /> 34 35 <metadata> 36 <status>PUBLISHED</status> 37 <categories>UI</categories> 38 <technologies>Android</technologies> 39 <languages>Java</languages> 40 <solutions>Mobile</solutions> 41 <level>INTERMEDIATE</level> 42 <icon>screenshots/icon-web.png</icon> 43 <screenshots> 44 <img>screenshots/screenshot-1.png</img> 45 </screenshots> 46 <api_refs> 47 <android>android.provider.FontRequest</android> 48 <android>android.support.v4.provider.FontRequest</android> 49 <android>android.provider.FontsContractCompat</android> 50 <android>android.support.v4.provider.FontsContractCompat</android> 51 </api_refs> 52 53 <description> 54<![CDATA[ 55This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 56Downloadable Fonts is a feature that allows apps to request a certain font from a provider 57instead of bundling it or downloading it themselves. This means, there is no need to bundle the 58font as an asset. 59]]> 60 </description> 61 62 <intro> 63<![CDATA[ 64There are two ways of requesting a font to download. 65To request a font to download from Java code, you need to create a [FontRequest][1] class first like 66this: 67```java 68FontRequest request = new FontRequest( 69 "com.google.android.gms.fonts", // ProviderAuthority 70 "com.google.android.gms", // ProviderPackage 71 query, // Query 72 R.array.com_google_android_gms_fonts_certs); // Certificates 73``` 74The parameters `ProviderAuthority`, `ProviderPackage` are given by a font provider, in the case 75above uses Google Play Services as a font provider. 76The third parameter is a query string about the requested font. The syntax of the query is defined 77by the font provider. 78 79Then pass the request instance to the `requestFont` method in the [FontsContractCompat][2]. 80```java 81FontsContractCompat.requestFont(context, request, callback, handler); 82``` 83The downloaded font or an error code if the request failed will be passed to the callback. 84The example above assumes you are using the classes from the support library. There are 85corresponding classes in the framework, but the feature is available back to API level 14 if you 86use the support library. 87 88You can declare a downloaded font in an XML file and let the system download it for you and use it 89in layouts. 90```xml 91<font-family xmlns:app="http://schemas.android.com/apk/res-auto" 92 app:fontProviderAuthority="com.google.android.gms.fonts" 93 app:fontProviderPackage="com.google.android.gms" 94 app:fontProviderQuery="Lobster Two" 95 app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"> 96</font-family> 97``` 98By defining the requested font in an XML file and putting the `preloaded_fonts` array and the 99meta-data tag in the AndroidManifest, you can avoid the delay until the font is downloaded by the 100first attempt. 101```xml 102<resources> 103 <array name="preloaded_fonts" translatable="false"> 104 <item>@font/lobster_two</item> 105 </array> 106</resources> 107``` 108 109```xml 110<application > 111 ... 112 <meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts" /> 113 ... 114</application> 115``` 116 117[1]: https://developer.android.com/reference/android/support/v4/provider/FontRequest.html 118[2]: https://developer.android.com/reference/android/support/v4/provider/FontsContractCompat.html 119]]> 120 </intro> 121 </metadata> 122</sample> 123