# DistanceSpan

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

*** ** * ** ***


```
@CarProtocol
public final class DistanceSpan 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.DistanceSpan](https://developer.android.com/reference/androidx/car/app/model/DistanceSpan) |

*** ** * ** ***

A span that replaces the text it is attached to with the string representation of a `https://developer.android.com/reference/androidx/car/app/model/Distance` instance.

The `https://developer.android.com/reference/androidx/car/app/model/Distance` instance will be displayed by the host in a localized format, so that it will be consistent with the rest of the user interface where distance information are displayed.

For example, the following code creates a string that shows the distance as the first text in the string before the interpunct:

```
String interpunct = "\\u00b7";
SpannableString string = new SpannableString("  " + interpunct + " Point-of-Interest 1");
string.setSpan(
  DistanceSpan.create(
    Distance.create(1000, "1.0", UNIT_KILOMETERS)), 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
```

The span flags (e.g. SPAN_EXCLUSIVE_EXCLUSIVE) will be ignored.

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`. However, it is possible to apply styling to the text, such as changing colors:

```
String interpunct = "\\u00b7";
SpannableString string = new SpannableString("  " + interpunct + " Point-of-Interest 1");
string.setSpan(
  DistanceSpan.create(
    Distance.create(1000, "1.0", UNIT_KILOMETERS)), 0, 1, SPAN_INCLUSIVE_INCLUSIVE);
string.setSpan(ForegroundCarColorSpan.create(CarColor.BLUE), 0, 1, SPAN_EXCLUSIVE_EXCLUSIVE);
```

## Summary

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/DistanceSpan` | `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan#create(androidx.car.app.model.Distance)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Distance distance)` Creates a `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon`. |
| `boolean` | `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan#equals(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/Object.html other)` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/car/app/model/Distance` | `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan#getDistance()()` Returns the `https://developer.android.com/reference/androidx/car/app/model/Distance` instance associated with this span. |
| `int` | `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan#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/DistanceSpan#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)` | |

## Public methods

### create

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

```
public static @NonNull DistanceSpan create(@NonNull Distance distance)
```

Creates a `https://developer.android.com/reference/androidx/car/app/model/DistanceSpan` from a `https://developer.android.com/reference/androidx/car/app/model/CarIcon`.

### equals

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

### getDistance

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

```
public @NonNull Distance getDistance()
```

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

### hashCode

```
public int hashCode()
```

### toString

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