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[ 31 This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 32 Downloadable Fonts is a feature that allows apps to request a certain font from a provider 33 instead of bundling it or downloading it themselves. This means, there is no need to bundle the 34 font as an asset. 35 36 Note that the sample uses Google Play Services as a font provider, which requires pre-released 37 version of Google Play Services. 38 You can sign up for the beta program so that the beta version of Google Play Services is 39 downloaded to your device. https://developers.google.com/android/guides/beta-program 40 If you have Google Play Services whose version number is equal or above 11.x.x, that means you 41 have the compatible version installed. (You can confirm by navigating to 42 Settings -> 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[ 69 This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. 70 Downloadable Fonts is a feature that allows apps to request a certain font from a provider 71 instead of bundling it or downloading it themselves. This means, there is no need to bundle the 72 font as an asset. 73 ]]> 74 </description> 75 76 <intro> 77 <![CDATA[ 78 There are two ways of requesting a font to download. 79 To request a font to download from Java code, you need to create a [FontRequest][1] class first like 80 this: 81 ```java 82 FontRequest 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 ``` 88 The parameters `ProviderAuthority`, `ProviderPackage` are given by a font provider, in the case 89 above uses Google Play Services as a font provider. 90 The third parameter is a query string about the requested font. The syntax of the query is defined 91 by the font provider. 92 93 Then pass the request instance to the `requestFont` method in the [FontsContractCompat][2]. 94 ```java 95 FontsContractCompat.requestFont(context, request, callback, handler); 96 ``` 97 The downloaded font or an error code if the request failed will be passed to the callback. 98 The example above assumes you are using the classes from the support library. There are 99 corresponding classes in the framework, but the feature is available back to API level 14 if you 100 use the support library. 101 102 You can declare a downloaded font in an XML file and let the system download it for you and use it 103 in 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 ``` 112 By defining the requested font in an XML file and putting the `preloaded_fonts` array and the 113 meta-data tag in the AndroidManifest, you can avoid the delay until the font is downloaded by the 114 first 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 131 Note that the sample uses Google Play Services as a font provider, which requires pre-released 132 version of Google Play Services. 133 You can sign up for the beta program so that the beta version of Google Play Services is 134 downloaded to your device. https://developers.google.com/android/guides/beta-program 135 If you have Google Play Services whose version number is equal or above 11.x.x, that means you 136 have the compatible version installed. (You can confirm by navigating to 137 Settings -> 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