{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

AndroidFont

public abstract class AndroidFont implements Font


Font for use on Android.

All AndroidFont produce an android.graphics.Typeface which may be used to draw text on Android. This is the main low-level API for introducing a new Font description to Compose on Android for both blocking and async load.

You may subclass this to add new types of font descriptions that may be used in FontListFontFamily. For example, you can add a FontLoadingStrategy.Blocking font that returns a Typeface from a local resource not supported by an existing Font. Or, you can create an FontLoadingStrategy.Async font that loads a font file from your server.

When introducing new font descriptors, it is recommended to follow the patterns of providing a public Font constructor and a private implementation class:

  1. Declare an internal or private subclass of AndroidFont

  2. Expose a public Font(...) constructor that returns your new type.

Font constructors are

  1. Regular functions named Font that return type Font

  2. The first argument is the font name, or similar object that describes the font uniquely

  3. If the font has a provider, loader, or similar argument, put it after the font name.

  4. The last two arguments are FontWeight and FontStyle.

Examples of Font constructors:

fun Font("myIdentifier", MyFontLoader, FontWeight, FontStyle): Font
fun Font(CustomFontDescription(...), MyFontLoader, FontWeight, FontStyle): Font
fun Font(CustomFontDescription(...), FontWeight, FontStyle): Font

Summary

Nested types

Loader for loading an AndroidFont and producing an android.graphics.Typeface.

Public constructors

AndroidFont(
    @NonNull FontLoadingStrategy loadingStrategy,
    @NonNull AndroidFont.TypefaceLoader typefaceLoader
)

This method is deprecated. Replaced with fontVariation constructor

AndroidFont(
    @NonNull FontLoadingStrategy loadingStrategy,
    @NonNull AndroidFont.TypefaceLoader typefaceLoader,
    @NonNull FontVariation.Settings variationSettings
)

Public methods

final @NonNull FontLoadingStrategy

loadingStrategy this font will provide in fallback chains

final @NonNull AndroidFont.TypefaceLoader

a loader that knows how to load this AndroidFont, may be shared between several fonts

final @NonNull FontVariation.Settings

The settings that will be applied to this font, if supported by the font.

Inherited methods

From androidx.compose.ui.text.font.Font
abstract @NonNull FontStyle

The style of the font, normal or italic.

abstract @NonNull FontWeight

The weight of the font.

Public constructors

AndroidFont

public AndroidFont(
    @NonNull FontLoadingStrategy loadingStrategy,
    @NonNull AndroidFont.TypefaceLoader typefaceLoader
)

AndroidFont

public AndroidFont(
    @NonNull FontLoadingStrategy loadingStrategy,
    @NonNull AndroidFont.TypefaceLoader typefaceLoader,
    @NonNull FontVariation.Settings variationSettings
)
Parameters
@NonNull FontLoadingStrategy loadingStrategy

loadingStrategy this font will provide in fallback chains

@NonNull AndroidFont.TypefaceLoader typefaceLoader

a loader that knows how to load this AndroidFont, may be shared between several fonts

Public methods

getLoadingStrategy

public final @NonNull FontLoadingStrategy getLoadingStrategy()

loadingStrategy this font will provide in fallback chains

getTypefaceLoader

public final @NonNull AndroidFont.TypefaceLoader getTypefaceLoader()

a loader that knows how to load this AndroidFont, may be shared between several fonts

getVariationSettings

public final @NonNull FontVariation.Settings getVariationSettings()

The settings that will be applied to this font, if supported by the font.

If the font does not support a FontVariation.Setting, it has no effect.

Subclasses are required to apply these variation settings during font loading path on appropriate API levels, for example by using Typeface.Builder.setFontVariationSettings.

Subclasses may safely apply all variation settings without querying the font file. Android will ignore any unsupported axis.