# Typeface.CustomFallbackBuilder

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# CustomFallbackBuilder

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/android/graphics/Typeface.CustomFallbackBuilder "View this page in Java")  

```
class CustomFallbackBuilder
```

|---|----------------------------------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html)                                                                     ||
| ↳ | [android.graphics.Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) |

A builder class for creating new Typeface instance. There are two font fallback mechanisms, custom font fallback and system font fallback. The custom font fallback is a simple ordered list. The text renderer tries to see if it can render a character with the first font and if that font does not support the character, try next one and so on. It will keep trying until end of the custom fallback chain. The maximum length of the custom fallback chain is 64. The system font fallback is a system pre-defined fallback chain. The system fallback is processed only when no matching font is found in the custom font fallback.

Examples, 1) Create Typeface from single ttf file.  

```kotlin
<code>
  Font font = new Font.Builder("your_font_file.ttf").build();
  FontFamily family = new FontFamily.Builder(font).build();
  Typeface typeface = new Typeface.CustomFallbackBuilder(family).build();
  </code>
```
2) Create Typeface from multiple font files and select bold style by default.  

```kotlin
<code>
  Font regularFont = new Font.Builder("regular.ttf").build();
  Font boldFont = new Font.Builder("bold.ttf").build();
  FontFamily family = new FontFamily.Builder(regularFont)
      .addFont(boldFont).build();
  Typeface typeface = new Typeface.CustomFallbackBuilder(family)
      .setWeight(Font.FONT_WEIGHT_BOLD)  // Set bold style as the default style.
                                         // If the font family doesn't have bold style font,
                                         // system will select the closest font.
      .build();
  </code>
```
3) Create Typeface from single ttf file and if that font does not have glyph for the characters, use "serif" font family instead.  

```kotlin
<code>
  Font font = new Font.Builder("your_font_file.ttf").build();
  FontFamily family = new FontFamily.Builder(font).build();
  Typeface typeface = new Typeface.CustomFallbackBuilder(family)
      .setSystemFallback("serif")  // Set serif font family as the fallback.
      .build();
  </code>
```
4) Create Typeface from single ttf file and set another ttf file for the fallback.  

```kotlin
<code>
  Font font = new Font.Builder("English.ttf").build();
  FontFamily family = new FontFamily.Builder(font).build();
 
  Font fallbackFont = new Font.Builder("Arabic.ttf").build();
  FontFamily fallbackFamily = new FontFamily.Builder(fallbackFont).build();
  Typeface typeface = new Typeface.CustomFallbackBuilder(family)
      .addCustomFallback(fallbackFamily)  // Specify fallback family.
      .setSystemFallback("serif")  // Set serif font family as the fallback.
      .build();
  </code>
```

<br />

## Summary

|                                                                                                                                                            Public constructors                                                                                                                                                             ||
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#CustomFallbackBuilder(android.graphics.fonts.FontFamily))`(`family:` `[FontFamily](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontFamily)`)` Constructs a builder with a font family. |

|                                                                                                                                                                                                                                     Public methods                                                                                                                                                                                                                                     ||
|-----------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | [addCustomFallback](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#addCustomFallback(android.graphics.fonts.FontFamily))`(`family:` `[FontFamily](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontFamily)`)` Append a font family to the end of the custom font fallback. |
| [Typeface](https://developer.android.com/reference/kotlin/android/graphics/Typeface)                                              | [build](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#build())`()` Create the Typeface based on the configured values.                                                                                                                                                                             |
| static[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)                                                  | [getMaxCustomFallbackCount](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#getMaxCustomFallbackCount())`()` Returns the maximum capacity of custom fallback families.                                                                                                                               |
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | [setStyle](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#setStyle(android.graphics.fonts.FontStyle))`(`style:` `[FontStyle](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontStyle)`)` Sets a font style of the Typeface.                                                 |
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | [setSystemFallback](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#setSystemFallback(kotlin.String))`(`familyName:` `[String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)`)` Sets a system fallback by name.                                                            |

## Public constructors

### CustomFallbackBuilder

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
CustomFallbackBuilder(family: FontFamily)
```

Constructs a builder with a font family.

|                                                                      Parameters                                                                      ||
|----------|--------------------------------------------------------------------------------------------------------------------------------------------|
| `family` | [FontFamily](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontFamily):a family object This value cannot be`null`. |

## Public methods

### addCustomFallback

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun addCustomFallback(family: FontFamily): Typeface.CustomFallbackBuilder
```

Append a font family to the end of the custom font fallback. You can set up to 64 custom fallback families including the first font family you passed to the constructor. For more information about fallback, see class description.

|                                                                       Parameters                                                                       ||
|----------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `family` | [FontFamily](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontFamily):a fallback family This value cannot be`null`. |

|                                                                             Return                                                                             ||
|-----------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | This value cannot be`null`. |

|                                       Exceptions                                        ||
|--------------------------------------|---------------------------------------------------|
| `java.lang.IllegalArgumentException` | if you give more than 64 custom fallback families |

### build

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun build(): Typeface
```

Create the Typeface based on the configured values.

|                                                                Return                                                                 ||
|--------------------------------------------------------------------------------------|-------------------------------------------------|
| [Typeface](https://developer.android.com/reference/kotlin/android/graphics/Typeface) | the Typeface object This value cannot be`null`. |

### getMaxCustomFallbackCount

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
static fun getMaxCustomFallbackCount(): Int
```

Returns the maximum capacity of custom fallback families. This includes the first font family passed to the constructor. It is guaranteed that the value will be greater than or equal to 64.

|                                                                             Return                                                                             ||
|----------------------------------------------------------------------------|------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html) | the maximum number of font families for the custom fallback Value is 64 or greater |

### setStyle

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun setStyle(style: FontStyle): Typeface.CustomFallbackBuilder
```

Sets a font style of the Typeface. If the font family doesn't have a font of given style, system will select the closest font from font family. For example, if a font family has fonts of 300 weight and 700 weight then setWeight(400) is called, system will select the font of 300 weight.

|                                                                   Parameters                                                                   ||
|---------|---------------------------------------------------------------------------------------------------------------------------------------|
| `style` | [FontStyle](https://developer.android.com/reference/kotlin/android/graphics/fonts/FontStyle):a font style This value cannot be`null`. |

|                                                                             Return                                                                             ||
|-----------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | This value cannot be`null`. |

### setSystemFallback

Added in[API level 29](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
fun setSystemFallback(familyName: String): Typeface.CustomFallbackBuilder
```

Sets a system fallback by name. You can specify generic font family names or OEM specific family names. If the system don't have a specified fallback, the default fallback is used instead. For more information about generic font families, see[CSS specification](https://www.w3.org/TR/css-fonts-4/#generic-font-families)For more information about fallback, see class description.

|                                                                                               Parameters                                                                                               ||
|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `familyName` | [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html):a family name to be used for fallback if the provided fonts can not be used This value cannot be`null`. |

|                                                                             Return                                                                             ||
|-----------------------------------------------------------------------------------------------------------------------------------|-----------------------------|
| [Typeface.CustomFallbackBuilder](https://developer.android.com/reference/kotlin/android/graphics/Typeface.CustomFallbackBuilder#) | This value cannot be`null`. |