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

*** ** * ** ***


```
public final class Alert.Builder
```

<br />

*** ** * ** ***

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

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#Builder(int,androidx.car.app.model.CarText,long)(int alertId, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarText title, long durationMillis)` Creates an `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` instance. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#addAction(androidx.car.app.model.Action)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action action)` Adds the `action` to the list of actions on the alert. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Alert` | `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#build()()` Constructs the `https://developer.android.com/reference/androidx/car/app/model/Alert` defined by this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#setCallback(androidx.car.app.model.AlertCallback)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/AlertCallback callback)` Sets the `https://developer.android.com/reference/androidx/car/app/model/AlertCallback` to receive alert related events. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#setIcon(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 icon)` Sets the icon to display in the alert. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder#setSubtitle(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 subtitle)` Sets the subtitle to display in the alert, with support for multiple length variants. |

## Public constructors

### Builder

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

```
public Builder(int alertId, @NonNull CarText title, long durationMillis)
```

Creates an `https://developer.android.com/reference/androidx/car/app/model/Alert.Builder` instance.

Text spans are supported.

| Parameters |
|---|---|
| `int alertId` | The unique identifier used for the alert. Alerts with the same id are considered equal. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarText title` | The title of the alert. |
| `long durationMillis` | The maximum duration the alert can be shown in milli seconds. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `title` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `duration` is not positive |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/model/CarText` |   |

## Public methods

### addAction

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

```
public @NonNull Alert.Builder addAction(@NonNull Action action)
```

Adds the `action` to the list of actions on the alert.

An alert can have up to 2 actions.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if more than 2 actions are added. |

### build

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

```
public @NonNull Alert build()
```

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

### setCallback

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

```
public @NonNull Alert.Builder setCallback(@NonNull AlertCallback callback)
```

Sets the `https://developer.android.com/reference/androidx/car/app/model/AlertCallback` to receive alert related events.

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

### setIcon

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

```
public @NonNull Alert.Builder setIcon(@NonNull CarIcon icon)
```

Sets the icon to display in the alert.

#### Icon Sizing Guidance

To minimize scaling artifacts across a wide range of car screens, apps should provide icons targeting a 88 x 88 dp bounding box. If the icon exceeds this maximum size in either one of the dimensions, it will be scaled down to be centered inside the bounding box while preserving its aspect ratio.

See `https://developer.android.com/reference/androidx/car/app/model/CarIcon` for more details related to providing icon and image resources that work with different car screen pixel densities.

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

### setSubtitle

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

```
public @NonNull Alert.Builder setSubtitle(@NonNull CarText subtitle)
```

Sets the subtitle to display in the alert, with support for multiple length variants.

Text spans are supported.

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

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/model/CarText` |   |