# CarIconSpan

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

*** ** * ** ***


```
@CarProtocol
public final class CarIconSpan extends CarSpan
```

<br />

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||||
| ↳ | [android.text.style.CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html) |||
|   | ↳ | [androidx.car.app.model.CarSpan](https://developer.android.com/reference/androidx/car/app/model/CarSpan) ||
|   |   | ↳ | [androidx.car.app.model.CarIconSpan](https://developer.android.com/reference/androidx/car/app/model/CarIconSpan) |

*** ** * ** ***

A span that replaces the text it is attached to with a `https://developer.android.com/reference/androidx/car/app/model/CarIcon` that is aligned with the surrounding text.

The image may be scaled with the text differently depending on the template that the text belongs to. Refer to the documentation of each template for that information.

For example, the following code creates a string for a navigation maneuver that has an image with the number of a highway rendered as an icon in between "on" and "East":

```
SpannableString string = new SpannableString("Turn right on 520 East");
string.setSpan(
    CarIconSpan.create(new CarIcon.Builder(
        IconCompat.createWithResource(getCarContext(), R.drawable.ic_520_highway))),
        14, 17, SPAN_INCLUSIVE_EXCLUSIVE);
```

`https://developer.android.com/reference/androidx/car/app/model/CarIconSpan`s in strings passed to the library templates may be ignored by the host when displaying the text unless support for them is explicitly documented in the API that takes the string.

This span will be ignored if it overlaps with any span that replaces text, such as another `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan`, `https://developer.android.com/reference/androidx/car/app/model/DurationSpan`, or `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan`.

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

## Summary

| ### Constants |
|---|---|
| `static final int` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BASELINE() = 1` A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text. |
| `static final int` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BOTTOM() = 0` A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, at the same level as the lowest descender in the text. |
| `static final int` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_CENTER() = 2` A constant indicating that this span should be vertically centered between the top and the lowest descender. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#create(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)` Creates a `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon` with a default alignment of `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BASELINE()`. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#create(androidx.car.app.model.CarIcon,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIcon icon, int alignment)` Creates a `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon`, specifying the alignment of the icon with respect to its surrounding text. |
| `boolean` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#equals(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html other)` |
| `int` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#getAlignment()()` Returns the alignment that should be used with this span. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIcon` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#getIcon()()` Returns the `https://developer.android.com/reference/androidx/car/app/model/CarIcon` instance associated with this span. |
| `int` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#hashCode()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#toString()()` |

| ### Inherited methods |
|---|
| From [androidx.car.app.model.CarSpan](https://developer.android.com/reference/androidx/car/app/model/CarSpan) |---|---| | `void` | `https://developer.android.com/reference/androidx/car/app/model/CarSpan#updateDrawState(android.text.TextPaint)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/text/TextPaint.html tp)` | |
| From [android.text.style.CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html) |---|---| | `https://developer.android.com/reference/android/text/style/CharacterStyle.html` | `https://developer.android.com/reference/android/text/style/CharacterStyle.html#getUnderlying()()` | | `abstract void` | `https://developer.android.com/reference/android/text/style/CharacterStyle.html#updateDrawState(android.text.TextPaint)(https://developer.android.com/reference/android/text/TextPaint.html p)` | | `static https://developer.android.com/reference/android/text/style/CharacterStyle.html` | `https://developer.android.com/reference/android/text/style/CharacterStyle.html#wrap(android.text.style.CharacterStyle)(https://developer.android.com/reference/android/text/style/CharacterStyle.html cs)` | |

## Constants

### ALIGN_BASELINE

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

```
public static final int ALIGN_BASELINE = 1
```

A constant indicating that the bottom of this span should be aligned with the baseline of the surrounding text.

### ALIGN_BOTTOM

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

```
public static final int ALIGN_BOTTOM = 0
```

A constant indicating that the bottom of this span should be aligned with the bottom of the surrounding text, at the same level as the lowest descender in the text.

### ALIGN_CENTER

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

```
public static final int ALIGN_CENTER = 2
```

A constant indicating that this span should be vertically centered between the top and the lowest descender.

## Public methods

### create

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

```
public static @NonNull CarIconSpan create(@NonNull CarIcon icon)
```

Creates a `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon` with a default alignment of `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BASELINE()`.

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

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#create(androidx.car.app.model.CarIcon,int)` |   |

### create

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

```
public static @NonNull CarIconSpan create(@NonNull CarIcon icon, int alignment)
```

Creates a `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon`, specifying the alignment of the icon with respect to its surrounding text.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/CarIcon icon` | the `https://developer.android.com/reference/androidx/car/app/model/CarIcon` to replace the text with |
| `int alignment` | the alignment of the `https://developer.android.com/reference/androidx/car/app/model/CarIcon` relative to the text. This should be one of `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BASELINE()`, `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BOTTOM()` or `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_CENTER()` |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `icon` is `null` |
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `alignment` is not a valid value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BASELINE()` |   |
| `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_BOTTOM()` |   |
| `https://developer.android.com/reference/androidx/car/app/model/CarIconSpan#ALIGN_CENTER()` |   |

### equals

```
public boolean equals(@Nullable Object other)
```

### getAlignment

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

```
public int getAlignment()
```

Returns the alignment that should be used with this span.

### getIcon

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

```
public @NonNull CarIcon getIcon()
```

Returns the `https://developer.android.com/reference/androidx/car/app/model/CarIcon` instance associated with this span.

### hashCode

```
public int hashCode()
```

### toString

```
public @NonNull String toString()
```