# FontsContractCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/provider/FontsContractCompat.java+class:androidx.core.provider.FontsContractCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***


```
public class FontsContractCompat
```

<br />

*** ** * ** ***

Utility class to deal with Font ContentProviders.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.Columns implements https://developer.android.com/reference/android/provider/BaseColumns.html` Defines the constants used in a response from a Font Provider. |
| `public class https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontFamilyResult` Object returned from `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#fetchFonts(android.content.Context,android.os.CancellationSignal,androidx.core.provider.FontRequest)`. |
| `public class https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontInfo` Object represent a font entry in the family returned from `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#fetchFonts(android.content.Context,android.os.CancellationSignal,androidx.core.provider.FontRequest)`. |
| `public class https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback` Interface used to receive asynchronously fetched typefaces. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#buildTypeface(android.content.Context,android.os.CancellationSignal,androidx.core.provider.FontsContractCompat.FontInfo[])( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html FontsContractCompat.FontInfo[] fonts )` Build a Typeface from an array of `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontInfo` Results that are marked as not ready will be skipped. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontFamilyResult` | `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#fetchFonts(android.content.Context,android.os.CancellationSignal,androidx.core.provider.FontRequest)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request )` Fetch fonts given a font request. |
| `static void` | `[requestFont](https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#requestFont(android.content.Context,androidx.core.provider.FontRequest,androidx.core.provider.FontsContractCompat.FontRequestCallback,android.os.Handler))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Handler.html handler )` **This method is deprecated.** due to the non-standard pattern of taking a handler for a non-UI thread - leading to both easily passing an incorrect handler and requiring all callers spin up an extra thread. <br /> |
| `static void` | `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#requestFont(android.content.Context,androidx.core.provider.FontRequest,int,java.util.concurrent.Executor,java.util.concurrent.Executor,androidx.core.provider.FontsContractCompat.FontRequestCallback)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request, int style, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html loadingExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback )` Request a font async as specified with `https://developer.android.com/reference/androidx/core/provider/FontRequest` Loading may take several seconds, and the `loadingExecutor` passed should be available to run blocking requests for several seconds. |
| `static void` | `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#requestFontWithFallbackChain(android.content.Context,java.util.List<androidx.core.provider.FontRequest>,int,java.util.concurrent.Executor,java.util.concurrent.Executor,androidx.core.provider.FontsContractCompat.FontRequestCallback)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/core/provider/FontRequest> requests, int style, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html loadingExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback )` Request a font async as specified with `https://developer.android.com/reference/androidx/core/provider/FontRequest` Loading may take several seconds, and the `loadingExecutor` passed should be available to run blocking requests for several seconds. |

## Public methods

### buildTypeface

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @Nullable Typeface buildTypeface(
    @NonNull Context context,
    @Nullable CancellationSignal cancellationSignal,
    @NonNull FontsContractCompat.FontInfo[] fonts
)
```

Build a Typeface from an array of `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontInfo` Results that are marked as not ready will be skipped.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html` that will be used to fetch the font contents. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal` | A signal to cancel the operation in progress, or null if none. If the operation is canceled, then `https://developer.android.com/reference/android/os/OperationCanceledException.html` will be thrown. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html FontsContractCompat.FontInfo[] fonts` | An array of `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontInfo` to be used to create a Typeface. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/graphics/Typeface.html` | A Typeface object. Returns null if typeface creation fails. |

### fetchFonts

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
public static @NonNull FontsContractCompat.FontFamilyResult fetchFonts(
    @NonNull Context context,
    @Nullable CancellationSignal cancellationSignal,
    @NonNull FontRequest request
)
```

Fetch fonts given a font request.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A `https://developer.android.com/reference/android/content/Context.html` to be used for fetching fonts. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/os/CancellationSignal.html cancellationSignal` | A signal to cancel the operation in progress, or null if none. If the operation is canceled, then `https://developer.android.com/reference/android/os/OperationCanceledException.html` will be thrown when the query is executed. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request` | A `https://developer.android.com/reference/androidx/core/provider/FontRequest` object that identifies the provider and query for the request. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontFamilyResult` | `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontFamilyResult` |

| Throws |
|---|---|
| `https://developer.android.com/reference/android/content/pm/PackageManager.NameNotFoundException.html` | If requested package or authority was not found in the system. |

### requestFont

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0) Deprecated in [1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)

```
public static void [requestFont](https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#requestFont(android.content.Context,androidx.core.provider.FontRequest,androidx.core.provider.FontsContractCompat.FontRequestCallback,android.os.Handler))(
    @NonNull Context context,
    @NonNull FontRequest request,
    @NonNull FontsContractCompat.FontRequestCallback callback,
    @NonNull Handler handler
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> due to the non-standard pattern of taking a handler for a non-UI thread - leading to both easily passing an incorrect handler and requiring all callers spin up an extra thread.

Create a typeface object given a font request. The font will be asynchronously fetched, therefore the result is delivered to the given callback. See `https://developer.android.com/reference/androidx/core/provider/FontRequest`. Only one of the methods in callback will be invoked, depending on whether the request succeeds or fails. These calls will happen on the main thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to be used for fetching from font provider. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request` | A `https://developer.android.com/reference/androidx/core/provider/FontRequest` object that identifies the provider and query for the request. May not be null. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback` | A callback that will be triggered when results are obtained. May not be null. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Handler.html handler` | A handler for running font fetch tasks on. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/core/provider/FontsContractCompat#requestFont(android.content.Context,androidx.core.provider.FontRequest,int,java.util.concurrent.Executor,java.util.concurrent.Executor,androidx.core.provider.FontsContractCompat.FontRequestCallback)` |   |

### requestFont

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)

```
public static void requestFont(
    @NonNull Context context,
    @NonNull FontRequest request,
    int style,
    @Nullable Executor loadingExecutor,
    @NonNull Executor callbackExecutor,
    @NonNull FontsContractCompat.FontRequestCallback callback
)
```

Request a font async as specified with `https://developer.android.com/reference/androidx/core/provider/FontRequest` Loading may take several seconds, and the `loadingExecutor` passed should be available to run blocking requests for several seconds. Results will be returned via `callbackExecutor`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to be used for fetching from font provider |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontRequest request` | A `https://developer.android.com/reference/androidx/core/provider/FontRequest` object that identifies the provider and query for the request. |
| `int style` | Typeface Style such as `https://developer.android.com/reference/android/graphics/Typeface.html#NORMAL`, `https://developer.android.com/reference/android/graphics/Typeface.html#BOLD``https://developer.android.com/reference/android/graphics/Typeface.html#ITALIC`, `https://developer.android.com/reference/android/graphics/Typeface.html#BOLD_ITALIC`. Value is one of the following: - `Typeface.NORMAL` - `Typeface.BOLD` - `Typeface.ITALIC` - `Typeface.BOLD_ITALIC` <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html loadingExecutor` | executor to load font on. Loading may take several _seconds_. If `null`, a default executor shared with other null-requests will be used. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor` | Used to dispatch callback |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback` | A callback that will be triggered when results are obtained. |

### requestFontWithFallbackChain

Added in [1.15.0](https://developer.android.com/jetpack/androidx/releases/core#1.15.0)

```
public static void requestFontWithFallbackChain(
    @NonNull Context context,
    @NonNull List<FontRequest> requests,
    int style,
    @Nullable Executor loadingExecutor,
    @NonNull Executor callbackExecutor,
    @NonNull FontsContractCompat.FontRequestCallback callback
)
```

Request a font async as specified with `https://developer.android.com/reference/androidx/core/provider/FontRequest` Loading may take several seconds, and the `loadingExecutor` passed should be available to run blocking requests for several seconds. Results will be returned via `callbackExecutor`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A context to be used for fetching from font provider |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/List.html<https://developer.android.com/reference/androidx/core/provider/FontRequest> requests` | An array of `https://developer.android.com/reference/androidx/core/provider/FontRequest` objects that identify the provider and query for the request, followed by any fallbacks. Fallbacks are used in the order specified. Note that the performance implications of font fallback scale with the number of fonts involved, so the length of this parameter should be kept to a minimum; we recommend no more than 2 (that is, the primary font and a single downloadable custom fallback). |
| `int style` | Typeface Style such as `https://developer.android.com/reference/android/graphics/Typeface.html#NORMAL`, `https://developer.android.com/reference/android/graphics/Typeface.html#BOLD``https://developer.android.com/reference/android/graphics/Typeface.html#ITALIC`, `https://developer.android.com/reference/android/graphics/Typeface.html#BOLD_ITALIC`. Value is one of the following: - `Typeface.NORMAL` - `Typeface.BOLD` - `Typeface.ITALIC` - `Typeface.BOLD_ITALIC` <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/util/concurrent/Executor.html loadingExecutor` | executor to load font on. Loading may take several _seconds_. If `null`, a default executor shared with other null-requests will be used. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html callbackExecutor` | Used to dispatch callback |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/provider/FontsContractCompat.FontRequestCallback callback` | A callback that will be triggered when results are obtained. |