# Chip.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/Chip.java+class:androidx.car.app.model.Chip.Builder) Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.8.0)

*** ** * ** ***


```
public final class Chip.Builder
```

<br />

*** ** * ** ***

A builder of `https://developer.android.com/reference/androidx/car/app/model/Chip`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#Builder()()` |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#build()()` Constructs the `https://developer.android.com/reference/androidx/car/app/model/Chip` defined by this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setEndIcon(androidx.car.app.model.CarIcon)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIcon endIcon)` Sets the icon to display at the end of the chip. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setOnClickListener(androidx.car.app.model.OnClickListener)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/OnClickListener onClickListener)` Sets the `https://developer.android.com/reference/androidx/car/app/model/OnClickListener` to be called when the chip is clicked. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setSelected(boolean)(boolean isSelected)` Sets the selected state of the chip, similar to how toggle has a selected state. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStartIcon(androidx.car.app.model.CarIcon)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIcon startIcon)` Sets the icon to display at the start of the chip. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStyle(androidx.car.app.model.ChipStyle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/ChipStyle style)` Sets the style of the chip. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setTitle(androidx.car.app.model.CarText)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarText title)` Sets the title of the chip, with support for multiple length variants. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Chip.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setTitle(java.lang.CharSequence)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/CharSequence.html title)` Sets the title of the chip. |

## Public constructors

### Builder

Added in 1.8.0

```
public Builder()
```

## Public methods

### build

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

```
public @NonNull Chip build()
```

Constructs the `https://developer.android.com/reference/androidx/car/app/model/Chip` defined by this builder.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the chip does not have a title, start icon, or end icon. |
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the chip does not have an `https://developer.android.com/reference/androidx/car/app/model/OnClickListener`. |

### setEndIcon

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

```
public @NonNull Chip.Builder setEndIcon(@NonNull CarIcon endIcon)
```

Sets the icon to display at the end of the chip.

The icon must conform to .

A chip without an endIcon **must** set `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setTitle(java.lang.CharSequence)` OR `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStartIcon(androidx.car.app.model.CarIcon)` or the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#build()` will throw an exception. Without an endIcon, the host will render the chip with the passed in title and/or startIcon (depending on which has been set).

The icon will attempt to use the most specific tint supplied; this is, it will try to use the first tint found in the following priority: 1. `https://developer.android.com/reference/androidx/car/app/model/CarIconStyle.Builder#setTint(androidx.car.app.model.CarColor)` set in `https://developer.android.com/reference/androidx/car/app/model/CarIcon.Builder#setStyle(androidx.car.app.model.CarIconStyle)` for the `startIcon` property 2. `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStyle(androidx.car.app.model.ChipStyle)``https://developer.android.com/reference/androidx/car/app/model/ChipStyle.Builder#setContentColor(androidx.car.app.model.CarColor)` 3. `https://developer.android.com/reference/androidx/car/app/model/ChipSection.Builder#setStyle(androidx.car.app.model.ChipStyle)` content color 4. A host supplied color that matches the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setSelected(boolean)` state Note that if the supplied color fails to meet contrast requirements, a host-supplied fallback that matches the selected state will be used.

Rasterized images such as (`https://developer.android.com/reference/androidx/core/graphics/drawable/IconCompat#TYPE_BITMAP()`) images will not have any tints **unless explicitly supplied through `https://developer.android.com/reference/androidx/car/app/model/CarIconStyle.Builder#setTint(androidx.car.app.model.CarColor)`.**.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `endIcon` is `null` |

### setOnClickListener

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

```
public @NonNull Chip.Builder setOnClickListener(@NonNull OnClickListener onClickListener)
```

Sets the `https://developer.android.com/reference/androidx/car/app/model/OnClickListener` to be called when the chip is clicked.

Note that the listener relates to the UI events and will be executed on the main thread using `https://developer.android.com/reference/android/os/Looper.html#getMainLooper()`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `onClickListener` is `null` |

### setSelected

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

```
public @NonNull Chip.Builder setSelected(boolean isSelected)
```

Sets the selected state of the chip, similar to how toggle has a selected state.

If the selected state is not set, the default will be false

If a custom style is provided through `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStyle(androidx.car.app.model.ChipStyle)`, changing this selected boolean value will not automatically change the color of this chip. The custom style must also be updated to reflect the selected state. See `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStyle(androidx.car.app.model.ChipStyle)` or `https://developer.android.com/reference/androidx/car/app/model/ChipSection.Builder#setStyle(androidx.car.app.model.ChipStyle)` for more details.

### setStartIcon

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

```
public @NonNull Chip.Builder setStartIcon(@NonNull CarIcon startIcon)
```

Sets the icon to display at the start of the chip.

The icon must conform to .

A chip without a startIcon **must** set `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setTitle(java.lang.CharSequence)` OR `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setEndIcon(androidx.car.app.model.CarIcon)` or the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#build()` will throw an exception. Without a startIcon, the host will render the chip with the passed in title and/or endIcon (depending on which has been set).

The icon will attempt to use the most specific tint supplied; this is, it will try to use the first tint found in the following priority: 1. `https://developer.android.com/reference/androidx/car/app/model/CarIconStyle.Builder#setTint(androidx.car.app.model.CarColor)` set in `https://developer.android.com/reference/androidx/car/app/model/CarIcon.Builder#setStyle(androidx.car.app.model.CarIconStyle)` for the `startIcon` property 2. `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStyle(androidx.car.app.model.ChipStyle)``https://developer.android.com/reference/androidx/car/app/model/ChipStyle.Builder#setContentColor(androidx.car.app.model.CarColor)` 3. `https://developer.android.com/reference/androidx/car/app/model/ChipSection.Builder#setStyle(androidx.car.app.model.ChipStyle)` content color 4. A host supplied color that matches the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setSelected(boolean)` state Note that if the supplied color fails to meet contrast requirements, a host-supplied fallback that matches the selected state will be used.

Rasterized images such as (`https://developer.android.com/reference/androidx/core/graphics/drawable/IconCompat#TYPE_BITMAP()`) images will not have any tints **unless explicitly supplied through `https://developer.android.com/reference/androidx/car/app/model/CarIconStyle.Builder#setTint(androidx.car.app.model.CarColor)`.**.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `startIcon` is `null` |

### setStyle

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

```
public @NonNull Chip.Builder setStyle(@NonNull ChipStyle style)
```

Sets the style of the chip.

This style overrides the style set in the `https://developer.android.com/reference/androidx/car/app/model/ChipSection` if one is provided. Any fields not explicitly set here or in the `https://developer.android.com/reference/androidx/car/app/model/ChipSection` will fall back to the host's default colors. If the style is not set or if the colors do not meet the contrast requirements, the host will set the chip styling to defaults based on the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setSelected(boolean)` state.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `style` is `null` |

### setTitle

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

```
public @NonNull Chip.Builder setTitle(@NonNull CarText title)
```

Sets the title of the chip, with support for multiple length variants.

A chip without a title **must** set `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStartIcon(androidx.car.app.model.CarIcon)` OR `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setEndIcon(androidx.car.app.model.CarIcon)` or the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#build()` will throw an exception. Without a title, the host will render the chip with the passed in startIcon and/or endIcon (depending on which has been set).

The text must conform to .

Developers can provide a shorter `https://developer.android.com/reference/androidx/car/app/model/CarText.Builder#addVariant(java.lang.CharSequence)` variant in case their title length exceeds the chip width limit enforced by the host.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `title` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `title` is empty, or if it contains unsupported spans |

### setTitle

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

```
public @NonNull Chip.Builder setTitle(@NonNull CharSequence title)
```

Sets the title of the chip.

A chip without a title **must** set `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setStartIcon(androidx.car.app.model.CarIcon)` OR `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#setEndIcon(androidx.car.app.model.CarIcon)` or the `https://developer.android.com/reference/androidx/car/app/model/Chip.Builder#build()` will throw an exception. Without a title, the host will render the chip with the passed in startIcon and/or endIcon (depending on which has been set).

The text must conform to .

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `title` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `title` is empty, or if it contains unsupported spans |