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 <!-- 23 Lower the minSdk once the API level for O is changed to 26. 24 At this moment, an app targeting "O" only runs on O preview devices. 25 --> 26 <minSdk>"O"</minSdk> 27 28 <strings> 29 <intro> 30<![CDATA[ 31This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 32Downloadable Fonts is a feature that allows apps to request a certain font from a provider 33instead of bundling it or downloading it themselves. This means, there is no need to bundle the 34font as an asset. 35 36Note that the sample uses Google Play Services as a font provider, which requires pre-released 37version of Google Play Services. 38You can sign up for the beta program so that the beta version of Google Play Services is 39downloaded to your device. https://developers.google.com/android/guides/beta-program 40If you have Google Play Services whose version number is equal or above 11.x.x, that means you 41have the compatible version installed. (You can confirm by navigating to 42Settings -> Apps -> Google Play Services) 43]]> 44 </intro> 45 </strings> 46 47 <template src="base-build" /> 48 49 <metadata> 50 <status>PUBLISHED</status> 51 <categories>UI, Android O Preview</categories> 52 <technologies>Android</technologies> 53 <languages>Java</languages> 54 <solutions>Mobile</solutions> 55 <level>INTERMEDIATE</level> 56 <icon>screenshots/icon-web.png</icon> 57 <screenshots> 58 <img>screenshots/screenshot-1.png</img> 59 </screenshots> 60 <api_refs> 61 <android>android.provider.FontRequest</android> 62 <android>android.support.v4.provider.FontRequest</android> 63 <android>android.provider.FontsContractCompat</android> 64 <android>android.support.v4.provider.FontsContractCompat</android> 65 </api_refs> 66 67 <description> 68<![CDATA[ 69This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 70Downloadable Fonts is a feature that allows apps to request a certain font from a provider 71instead of bundling it or downloading it themselves. This means, there is no need to bundle the 72font as an asset. 73]]> 74 </description> 75 76 <intro> 77<![CDATA[ 78There are two ways of requesting a font to download. 79To request a font to download from Java code, you need to create a [FontRequest][1] class first like 80this: 81```java 82FontRequest request = new FontRequest( 83 "com.google.android.gms.fonts", // ProviderAuthority 84 "com.google.android.gms", // ProviderPackage 85 query, // Query 86 R.array.com_google_android_gms_fonts_certs); // Certificates 87``` 88The parameters `ProviderAuthority`, `ProviderPackage` are given by a font provider, in the case 89above uses Google Play Services as a font provider. 90The third parameter is a query string about the requested font. The syntax of the query is defined 91by the font provider. 92 93Then pass the request instance to the `requestFont` method in the [FontsContractCompat][2]. 94```java 95FontsContractCompat.requestFont(context, request, callback, handler); 96``` 97The downloaded font or an error code if the request failed will be passed to the callback. 98The example above assumes you are using the classes from the support library. There are 99corresponding classes in the framework, but the feature is available back to API level 14 if you 100use the support library. 101 102You can declare a downloaded font in an XML file and let the system download it for you and use it 103in layouts. 104```xml 105<font-family xmlns:app="http://schemas.android.com/apk/res-auto" 106 app:fontProviderAuthority="com.google.android.gms.fonts" 107 app:fontProviderPackage="com.google.android.gms" 108 app:fontProviderQuery="Lobster Two" 109 app:fontProviderCerts="@array/com_google_android_gms_fonts_certs"> 110</font-family> 111``` 112By defining the requested font in an XML file and putting the `preloaded_fonts` array and the 113meta-data tag in the AndroidManifest, you can avoid the delay until the font is downloaded by the 114first attempt. 115```xml 116<resources> 117 <array name="preloaded_fonts" translatable="false"> 118 <item>@font/lobster_two</item> 119 </array> 120</resources> 121``` 122 123```xml 124<application > 125 ... 126 <meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts" /> 127 ... 128</application> 129``` 130 131Note that the sample uses Google Play Services as a font provider, which requires pre-released 132version of Google Play Services. 133You can sign up for the beta program so that the beta version of Google Play Services is 134downloaded to your device. https://developers.google.com/android/guides/beta-program 135If you have Google Play Services whose version number is equal or above 11.x.x, that means you 136have the compatible version installed. (You can confirm by navigating to 137Settings -> Apps -> Google Play Services) 138 139[1]: https://developer.android.com/reference/android/support/v4/provider/FontRequest.html 140[2]: https://developer.android.com/reference/android/support/v4/provider/FontsContractCompat.html 141]]> 142 </intro> 143 </metadata> 144</sample> 145