# CarToast

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

*** ** * ** ***


```
public final class CarToast
```

<br />

*** ** * ** ***

A message to show to the user for a short period of time.

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_LONG() = 1` Show the Toast view for a long period of time. |
| `static final int` | `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_SHORT() = 0` Show the Toast view for a short period of time. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarToast` | `https://developer.android.com/reference/androidx/car/app/CarToast#makeText(androidx.car.app.CarContext,java.lang.CharSequence,int)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarContext carContext, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/CharSequence.html text, int duration )` Creates and sets the text and duration for the toast view. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarToast` | `https://developer.android.com/reference/androidx/car/app/CarToast#makeText(androidx.car.app.CarContext,int,int)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarContext carContext, @https://developer.android.com/reference/androidx/annotation/StringRes int textResId, int duration )` Creates and sets the text and duration for the toast view. |
| `void` | `https://developer.android.com/reference/androidx/car/app/CarToast#setDuration(int)(int duration)` Sets how long to show the toast for. |
| `void` | `https://developer.android.com/reference/androidx/car/app/CarToast#setText(java.lang.CharSequence)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/CharSequence.html text)` Sets the text for the toast. |
| `void` | `https://developer.android.com/reference/androidx/car/app/CarToast#setText(int)(@https://developer.android.com/reference/androidx/annotation/StringRes int textResId)` Sets the text for the toast. |
| `void` | `https://developer.android.com/reference/androidx/car/app/CarToast#show()()` Shows the toast with the specified text for the specified duration. |

## Constants

### LENGTH_LONG

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

```
public static final int LENGTH_LONG = 1
```

Show the Toast view for a long period of time.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/CarToast#setDuration(int)` |   |

### LENGTH_SHORT

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

```
public static final int LENGTH_SHORT = 0
```

Show the Toast view for a short period of time. This is the default duration.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/CarToast#setDuration(int)` |   |

## Public methods

### makeText

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

```
public static @NonNull CarToast makeText(
    @NonNull CarContext carContext,
    @NonNull CharSequence text,
    int duration
)
```

Creates and sets the text and duration for the toast view.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarContext carContext` | the CarContext providing the service used to show the toast |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/CharSequence.html text` | the text to show |
| `int duration` | how long to display the message. Either `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_SHORT()` or `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_LONG()` |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if either the `carContext` or the `text` are ` null` |

### makeText

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

```
public static @NonNull CarToast makeText(
    @NonNull CarContext carContext,
    @StringRes int textResId,
    int duration
)
```

Creates and sets the text and duration for the toast view.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/CarContext carContext` | the context used for string localization |
| `@https://developer.android.com/reference/androidx/annotation/StringRes int textResId` | the resource id for the text to show. If the `textResId` is 0, the text will be set to empty |
| `int duration` | how long to display the message. Either `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_SHORT()` or `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_LONG()` |

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

### setDuration

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

```
public void setDuration(int duration)
```

Sets how long to show the toast for.

| Parameters |
|---|---|
| `int duration` | how long to display the message. Either `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_SHORT()` or `https://developer.android.com/reference/androidx/car/app/CarToast#LENGTH_LONG()` |

### setText

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

```
public void setText(@NonNull CharSequence text)
```

Sets the text for the toast.

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

### setText

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

```
public void setText(@StringRes int textResId)
```

Sets the text for the toast.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/StringRes int textResId` | the resource id for the text. If the `textResId` is 0, the text will be set to empty |

### show

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

```
public void show()
```

Shows the toast with the specified text for the specified duration.

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/HostException` | if the remote call fails |