Coder Social home page Coder Social logo

android-downloadablefonts's Introduction

Android DownloadableFonts Sample

This sample demonstrates how to use the Downloadable Fonts feature introduced in Android O. Downloadable Fonts is a feature that allows apps to request a certain font from a provider instead of bundling it or downloading it themselves. This means, there is no need to bundle the font as an asset.

Introduction

There are two ways of requesting a font to download. To request a font to download from Java code, you need to create a FontRequest class first like this:

FontRequest request = new FontRequest(
    "com.google.android.gms.fonts", // ProviderAuthority
    "com.google.android.gms",  // ProviderPackage
    query,  // Query
    R.array.com_google_android_gms_fonts_certs); // Certificates

The parameters ProviderAuthority, ProviderPackage are given by a font provider, in the case above uses Google Play Services as a font provider. The third parameter is a query string about the requested font. The syntax of the query is defined by the font provider.

Then pass the request instance to the requestFont method in the FontsContractCompat.

FontsContractCompat.requestFont(context, request, callback, handler);

The downloaded font or an error code if the request failed will be passed to the callback. The example above assumes you are using the classes from the support library. There are corresponding classes in the framework, but the feature is available back to API level 14 if you use the support library.

You can declare a downloaded font in an XML file and let the system download it for you and use it in layouts.

<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
        app:fontProviderAuthority="com.google.android.gms.fonts"
        app:fontProviderPackage="com.google.android.gms"
        app:fontProviderQuery="Lobster Two"
        app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

By defining the requested font in an XML file and putting the preloaded_fonts array and the meta-data tag in the AndroidManifest, you can avoid the delay until the font is downloaded by the first attempt.

<resources>
    <array name="preloaded_fonts" translatable="false">
        <item>@font/lobster_two</item>
    </array>
</resources>
<application >
    ...
    <meta-data android:name="preloaded_fonts" android:resource="@array/preloaded_fonts" />
    ...
</application>

Note that the sample uses Google Play Services as a font provider, which requires pre-released version of Google Play Services. You can sign up for the beta program so that the beta version of Google Play Services is downloaded to your device. https://developers.google.com/android/guides/beta-program If you have Google Play Services whose version number is equal or above 11.x.x, that means you have the compatible version installed. (You can confirm by navigating to Settings -> Apps -> Google Play Services)

Pre-requisites

  • Android SDK 25
  • Android Build Tools v25.0.3
  • Android Support Repository

Screenshots

Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-DownloadableFonts

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

android-downloadablefonts's People

Contributors

google-automerger avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.