# DistanceSpan

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/car/app/model/DistanceSpan "View this page in Java")

<br />

```
@CarProtocol
class DistanceSpan : CarSpan
```

<br />

|---|---|---|---------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.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/kotlin/androidx/car/app/model/CarSpan)              ||
|   |   | ↳ | [androidx.car.app.model.DistanceSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan) |

*** ** * ** ***

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

The[Distance](https://developer.android.com/reference/kotlin/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:  

```kotlin
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[DistanceSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan),[DurationSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DurationSpan), or[CarIconSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarIconSpan). However, it is possible to apply styling to the text, such as changing colors:  

```kotlin
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 functions                                               |
|------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[DistanceSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan) | [create](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan#create(androidx.car.app.model.Distance))`(distance: `[Distance](https://developer.android.com/reference/kotlin/androidx/car/app/model/Distance)`)` Creates a[DistanceSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan)from a[CarIcon](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarIcon). |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                              | [equals](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan#equals(java.lang.Object))`(other: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`?)`                                                                                                                                                                                                                                            |
| [Distance](https://developer.android.com/reference/kotlin/androidx/car/app/model/Distance)                       | [getDistance](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan#getDistance())`()` Returns the[Distance](https://developer.android.com/reference/kotlin/androidx/car/app/model/Distance)instance associated with this span.                                                                                                                                                                                              |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                      | [hashCode](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan#hashCode())`()`                                                                                                                                                                                                                                                                                                                                             |
| [String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                                | [toString](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan#toString())`()`                                                                                                                                                                                                                                                                                                                                             |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ### Inherited functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.car.app.model.CarSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarSpan) |-------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [updateDrawState](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarSpan#updateDrawState(android.text.TextPaint))`(tp: `[TextPaint](https://developer.android.com/reference/android/text/TextPaint.html)`)` |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| From[android.text.style.CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html) |-------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html)`!`               | [getUnderlying](https://developer.android.com/reference/android/text/style/CharacterStyle.html#getUnderlying())`()`                                                                                                                       | | `abstract `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                          | [updateDrawState](https://developer.android.com/reference/android/text/style/CharacterStyle.html#updateDrawState(android.text.TextPaint))`(p: `[TextPaint](https://developer.android.com/reference/android/text/TextPaint.html)`!)`       | | `java-static `[CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html)`!` | [wrap](https://developer.android.com/reference/android/text/style/CharacterStyle.html#wrap(android.text.style.CharacterStyle))`(cs: `[CharacterStyle](https://developer.android.com/reference/android/text/style/CharacterStyle.html)`!)` | |

## Public functions

### create

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

```
java-static fun create(distance: Distance): DistanceSpan
```

Creates a[DistanceSpan](https://developer.android.com/reference/kotlin/androidx/car/app/model/DistanceSpan)from a[CarIcon](https://developer.android.com/reference/kotlin/androidx/car/app/model/CarIcon).  

### equals

```
fun equals(other: Any?): Boolean
```  

### getDistance

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

```
fun getDistance(): Distance
```

Returns the[Distance](https://developer.android.com/reference/kotlin/androidx/car/app/model/Distance)instance associated with this span.  

### hashCode

```
fun hashCode(): Int
```  

### toString

```
fun toString(): String
```