# CarText.Builder

# CarText.Builder

Artifact:[androidx.car.app:app](https://developer.android.com/jetpack/androidx/releases/car-app)  
[View Source](https://cs.android.com/search?q=file:androidx/car/app/model/CarText.java+class:androidx.car.app.model.CarText.Builder)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/car/app/model/CarText.Builder "View this page in Java")

<br />

```
@CarProtocol
class CarText.Builder
```

<br />

*** ** * ** ***

A builder of[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText).

## Summary

|                                                                                                                                                                         ### Public constructors                                                                                                                                                                         |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Builder](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#Builder(java.lang.CharSequence))`(text: `[CharSequence](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html)`)` Returns a new instance of a[Builder](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder). |

|                                           ### Public functions                                           |
|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [CarText.Builder](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder) | `@`[RequiresCarApi](https://developer.android.com/reference/kotlin/androidx/car/app/annotations/RequiresCarApi)`(value = 2)` [addVariant](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#addVariant(java.lang.CharSequence))`(text: `[CharSequence](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html)`)` Adds a text variant for the[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText)instance. |
| [CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText)                 | [build](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#build())`()` Constructs the[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText)defined by this builder.                                                                                                                                                                                                                                                                  |

## Public constructors

### Builder

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)  

```
Builder(text: CharSequence)
```

Returns a new instance of a[Builder](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder).

Only[CarSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarSpan)type spans are allowed in a[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText), other spans will be removed from the provided[CharSequence](https://developer.android.com/reference/java/lang/CharSequence.html).  

|                                               Parameters                                               |
|--------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `text: `[CharSequence](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html) | the first variant of the text to use. This represents the app's preferred text variant. Other alternatives can be supplied with[addVariant](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#addVariant(java.lang.CharSequence)). |

|                                                    Throws                                                     |
|---------------------------------------------------------------------------------------------------------------|----------------------|
| [java.lang.NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException.html) | if the text is`null` |

|                                                                See also                                                                |
|----------------------------------------------------------------------------------------------------------------------------------------|---|
| [addVariant](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#addVariant(java.lang.CharSequence)) |   |

## Public functions

### addVariant

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)  

```
@RequiresCarApi(value = 2)
fun addVariant(text: CharSequence): CarText.Builder
```

Adds a text variant for the[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText)instance.

Only[CarSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarSpan)type spans are allowed in a[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText), other spans will be removed from the provided[CharSequence](https://developer.android.com/reference/java/lang/CharSequence.html).

The text variants should be added in order of preference, from most to least preferred (for instance, from longest to shortest). If the text provided via[Builder](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText.Builder#Builder(java.lang.CharSequence))does not fit in the screen, the host will display the first variant that fits in the screen.

For instance, if the variant order is \["long string", "shorter", "short"\], and the screen can fit 7 characters, "shorter" will be chosen. However, if the order is \["short", "shorter", "long string"\], "short" will be chosen, because "short" fits within the 7 character limit.  

|                                                    Throws                                                     |
|---------------------------------------------------------------------------------------------------------------|----------------------|
| [java.lang.NullPointerException](https://developer.android.com/reference/java/lang/NullPointerException.html) | if the text is`null` |

### build

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.0.0)  

```
fun build(): CarText
```

Constructs the[CarText](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarText)defined by this builder.