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

*** ** * ** ***


```
public final class Action.Builder
```

<br />

*** ** * ** ***

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

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/car/app/model/Action.Builder#Builder()()` Creates an empty `https://developer.android.com/reference/androidx/car/app/model/Action.Builder` instance. |
| `@https://developer.android.com/reference/androidx/car/app/annotations/RequiresCarApi(value = 2) https://developer.android.com/reference/androidx/car/app/model/Action.Builder#Builder(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)` Returns a `https://developer.android.com/reference/androidx/car/app/model/Action.Builder` instance configured with the same data as the given `https://developer.android.com/reference/androidx/car/app/model/Action` instance. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action` | `https://developer.android.com/reference/androidx/car/app/model/Action.Builder#build()()` Constructs the `https://developer.android.com/reference/androidx/car/app/model/Action` defined by this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Action.Builder#setBackgroundColor(androidx.car.app.model.CarColor)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarColor backgroundColor)` Sets the background color to be used for the action. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action.Builder` | `@https://developer.android.com/reference/androidx/car/app/annotations/RequiresCarApi(value = 5) https://developer.android.com/reference/androidx/car/app/model/Action.Builder#setEnabled(boolean)(boolean enabled)` Sets the initial enabled state for `https://developer.android.com/reference/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.Builder` | `@https://developer.android.com/reference/androidx/car/app/annotations/RequiresCarApi(value = 4) https://developer.android.com/reference/androidx/car/app/model/Action.Builder#setFlags(int)(int flags)` Sets flags affecting how this action should be treated. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Action.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 action. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Action.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 listener)` Sets the `https://developer.android.com/reference/androidx/car/app/model/OnClickListener` to call when the action is clicked. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Action.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Action.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 to display in the action, 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/Action.Builder` | `https://developer.android.com/reference/androidx/car/app/model/Action.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 to display in the action. |

## Public constructors

### Builder

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

```
public Builder()
```

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

### Builder

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

```
@RequiresCarApi(value = 2)
public Builder(@NonNull Action action)
```

Returns a `https://developer.android.com/reference/androidx/car/app/model/Action.Builder` instance configured with the same data as the given `https://developer.android.com/reference/androidx/car/app/model/Action` instance.

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

## Public methods

### build

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

```
public @NonNull Action build()
```

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

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the action is not a standard action and does not have an icon or a title, if a listener is set on either `https://developer.android.com/reference/androidx/car/app/model/Action#APP_ICON()` or `https://developer.android.com/reference/androidx/car/app/model/Action#BACK()`, or if an icon or title is set on either `https://developer.android.com/reference/androidx/car/app/model/Action#APP_ICON()` or `https://developer.android.com/reference/androidx/car/app/model/Action#BACK()` |

### setBackgroundColor

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

```
public @NonNull Action.Builder setBackgroundColor(@NonNull CarColor backgroundColor)
```

Sets the background color to be used for the action.

#### Requirements

Depending on contrast requirements, capabilities of the vehicle screens, or other factors, the color may be ignored by the host or overridden by the vehicle system. See the documentation on where the `https://developer.android.com/reference/androidx/car/app/model/Action` is added for more details on any other restriction(s) that might apply.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarColor backgroundColor` | the `https://developer.android.com/reference/androidx/car/app/model/CarColor` to set as background. Use `https://developer.android.com/reference/androidx/car/app/model/CarColor#DEFAULT()` to let the host pick a default |

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

### setEnabled

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

```
@RequiresCarApi(value = 5)
public @NonNull Action.Builder setEnabled(boolean enabled)
```

Sets the initial enabled state for `https://developer.android.com/reference/androidx/car/app/model/Action`.

The default state of a `https://developer.android.com/reference/androidx/car/app/model/Action` is enabled.

### setFlags

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

```
@RequiresCarApi(value = 4)
public @NonNull Action.Builder setFlags(int flags)
```

Sets flags affecting how this action should be treated.

### setIcon

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

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

Sets the icon to display in the action.

Unless set with this method, the action will not have an icon.

#### 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` |

### setOnClickListener

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

```
public @NonNull Action.Builder setOnClickListener(@NonNull OnClickListener listener)
```

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

Unless set with this method, the action will not have a click listener.

Note that the listener relates to 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 `listener` is `null` |

### setTitle

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

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

Sets the title to display in the action, with support for multiple length variants.

Support for text spans depends on where the action is used. For example, most templates taking an action support `https://developer.android.com/reference/androidx/car/app/model/ForegroundCarColorSpan`, but this may vary. See the documentation of the specific APIs taking an `https://developer.android.com/reference/androidx/car/app/model/Action` for details.

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

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

### setTitle

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

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

Sets the title to display in the action.

Support for text spans depends on where the action is used. See the documentation of the specific APIs taking an `https://developer.android.com/reference/androidx/car/app/model/Action` for details.

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