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

*** ** * ** ***


```
public final class TabTemplate.Builder
```

<br />

*** ** * ** ***

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

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#Builder(androidx.car.app.model.TabTemplate.TabCallback)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.TabCallback callback)` Creates a `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` instance using the given `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.TabCallback`. |
| `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#Builder(androidx.car.app.model.TabTemplate)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate tabTemplate)` Creates a new `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder`, populated from the input `https://developer.android.com/reference/androidx/car/app/model/TabTemplate` |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#addTab(androidx.car.app.model.Tab)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Tab tab)` Adds an `https://developer.android.com/reference/androidx/car/app/model/Tab` to display in the template. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#build()()` Constructs the template defined by this builder. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setActiveTabContentId(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html contentId)` Stores the given `contentId` as the "active tab" to show on the screen. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `@https://developer.android.com/reference/androidx/car/app/annotations/RequiresCarApi(value = 9) @https://developer.android.com/reference/androidx/car/app/annotations/ExperimentalCarApi https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setEndAction(androidx.car.app.model.Action)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/car/app/model/Action endAction)` Sets an `https://developer.android.com/reference/androidx/car/app/model/Action` to be displayed at the end of the header/tab bar, or `null` to clear the end action. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setHeaderAction(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 headerAction)` Sets the `https://developer.android.com/reference/androidx/car/app/model/Action` that will be displayed in the header of the template, or `null` to not display an action. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setLoading(boolean)(boolean isLoading)` Sets whether the template is in a loading state. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `@https://developer.android.com/reference/androidx/car/app/annotations/RequiresCarApi(value = 9) @https://developer.android.com/reference/androidx/car/app/annotations/ExperimentalCarApi https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setStyle(androidx.car.app.model.TabStyle)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/car/app/model/TabStyle tabStyle)` Sets the `https://developer.android.com/reference/androidx/car/app/model/TabStyle` for all tabs in this template, or `null` to clear the style. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` | `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#setTabContents(androidx.car.app.model.TabContents)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabContents tabContents)` Sets the `https://developer.android.com/reference/androidx/car/app/model/TabContents` to show in the template. |

## Public constructors

### Builder

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

```
public Builder(@NonNull TabTemplate.TabCallback callback)
```

Creates a `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder` instance using the given `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.TabCallback`.

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

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/TabTemplate.TabCallback callback` | the callback to be invoked when the user selects a new tab in the header |

### Builder

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

```
public Builder(@NonNull TabTemplate tabTemplate)
```

Creates a new `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder`, populated from the input `https://developer.android.com/reference/androidx/car/app/model/TabTemplate`

## Public methods

### addTab

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

```
public @NonNull TabTemplate.Builder addTab(@NonNull Tab tab)
```

Adds an `https://developer.android.com/reference/androidx/car/app/model/Tab` to display in the template.

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

### build

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

```
public @NonNull TabTemplate build()
```

Constructs the template defined by this builder.

#### Requirements

The number of `https://developer.android.com/reference/androidx/car/app/model/Tab`s provided in the template should be between 2 and 4, with only one tab marked as active.

A header `https://developer.android.com/reference/androidx/car/app/model/Action` of type TYPE_APP_ICON is required.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalStateException.html` | if the template is in a loading state but there are tabs added or vice versa |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if the added `https://developer.android.com/reference/androidx/car/app/model/Tab`(s) or header `https://developer.android.com/reference/androidx/car/app/model/Action` does not meet the template's requirements |

### setActiveTabContentId

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

```
public @NonNull TabTemplate.Builder setActiveTabContentId(@NonNull String contentId)
```

Stores the given `contentId` as the "active tab" to show on the screen. The given ID must match a tab that was added by `https://developer.android.com/reference/androidx/car/app/model/TabTemplate.Builder#addTab(androidx.car.app.model.Tab)`.

### setEndAction

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

```
@RequiresCarApi(value = 9)
@ExperimentalCarApi
public @NonNull TabTemplate.Builder setEndAction(@Nullable Action endAction)
```

Sets an `https://developer.android.com/reference/androidx/car/app/model/Action` to be displayed at the end of the header/tab bar, or `null` to clear the end action.

#### Requirements

The end action displayed in the tab bar must have an icon and must not have a title.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `endAction` does not meet the template's requirements |

### setHeaderAction

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

```
public @NonNull TabTemplate.Builder setHeaderAction(@NonNull Action headerAction)
```

Sets the `https://developer.android.com/reference/androidx/car/app/model/Action` that will be displayed in the header of the template, or `null` to not display an action.

Unless set with this method, the template will not have a header action.

#### Requirements

This template only supports `https://developer.android.com/reference/androidx/car/app/model/Action#APP_ICON()` as a header `https://developer.android.com/reference/androidx/car/app/model/Action`.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `headerAction` does not meet the template's requirements |
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `headerAction` is `null` |

### setLoading

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

```
public @NonNull TabTemplate.Builder setLoading(boolean isLoading)
```

Sets whether the template is in a loading state.

If set to `true`, the UI will display a loading indicator where the content would be otherwise. The caller is expected to call `https://developer.android.com/reference/androidx/car/app/Screen#invalidate()` and send the new template content to the host once the data is ready.

If set to `false`, the UI will display the contents of the template.

### setStyle

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

```
@RequiresCarApi(value = 9)
@ExperimentalCarApi
public @NonNull TabTemplate.Builder setStyle(@Nullable TabStyle tabStyle)
```

Sets the `https://developer.android.com/reference/androidx/car/app/model/TabStyle` for all tabs in this template, or `null` to clear the style.

Any fields not explicitly set here or in the individual tab styling of `https://developer.android.com/reference/androidx/car/app/model/Tab.Builder#setStyle(androidx.car.app.model.TabStyle)` will fall back to host defaults.

### setTabContents

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

```
public @NonNull TabTemplate.Builder setTabContents(@NonNull TabContents tabContents)
```

Sets the `https://developer.android.com/reference/androidx/car/app/model/TabContents` to show in the template. Note that only certain templates may be used as content. See `https://developer.android.com/reference/androidx/car/app/model/TabContents.Builder#Builder(androidx.car.app.model.Template)` for more details.

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