# SpanUtil

Artifact: [androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/common/text/SpanUtil.java+class:androidx.media3.common.text.SpanUtil)

*** ** * ** ***


```
@UnstableApi
public final class SpanUtil
```

<br />

*** ** * ** ***

Utility methods for Android [span styling](https://developer.android.com/guide/topics/text/spans).

## Summary

| ### Public methods |
|---|---|
| `static void` | `https://developer.android.com/reference/androidx/media3/common/text/SpanUtil#addInheritedRelativeSizeSpan(android.text.Spannable,float,int,int,int)( https://developer.android.com/reference/android/text/Spannable.html spannable, float size, int start, int end, int spanFlags )` Modifies the size of the text between `start` and `end` relative to any existing `https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instances which cover **at least the same range**. |
| `static void` | `https://developer.android.com/reference/androidx/media3/common/text/SpanUtil#addOrReplaceSpan(android.text.Spannable,java.lang.Object,int,int,int)( https://developer.android.com/reference/android/text/Spannable.html spannable, https://developer.android.com/reference/java/lang/Object.html span, int start, int end, int spanFlags )` Adds `span` to `spannable` between `start` and `end`, removing any existing spans of the same type and with the same indices and flags. |

## Public methods

### addInheritedRelativeSizeSpan

```
public static void addInheritedRelativeSizeSpan(
    Spannable spannable,
    float size,
    int start,
    int end,
    int spanFlags
)
```

Modifies the size of the text between `start` and `end` relative to any existing `https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instances which cover **at least the same range**.

`https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instances which only cover a part of the text between `start` and `end` are ignored.

A new `https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instance is added between `start` and `end` with its `sizeChange` value computed by modifying the `size` parameter by the `
sizeChange` of `https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instances covering between `start` and `
end`.

`https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` instances with the same `start`, `end`, and `
spanFlags` are removed.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/text/Spannable.html spannable` | The `https://developer.android.com/reference/android/text/Spannable.html` to add the `https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html` to. |
| `float size` | The fraction to modify the text size by. |
| `int start` | The start index to add the new span at. |
| `int end` | The end index to add the new span at. |
| `int spanFlags` | The flags to pass to `https://developer.android.com/reference/android/text/Spannable.html#setSpan(java.lang.Object, int, int, int)`. |

### addOrReplaceSpan

```
public static void addOrReplaceSpan(
    Spannable spannable,
    Object span,
    int start,
    int end,
    int spanFlags
)
```

Adds `span` to `spannable` between `start` and `end`, removing any existing spans of the same type and with the same indices and flags.

This is useful for types of spans that don't make sense to duplicate and where the evaluation order might have an unexpected impact on the final text, e.g. `https://developer.android.com/reference/android/text/style/ForegroundColorSpan.html`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/text/Spannable.html spannable` | The `https://developer.android.com/reference/android/text/Spannable.html` to add `span` to. |
| `https://developer.android.com/reference/java/lang/Object.html span` | The span object to be added. |
| `int start` | The start index to add the new span at. |
| `int end` | The end index to add the new span at. |
| `int spanFlags` | The flags to pass to `https://developer.android.com/reference/android/text/Spannable.html#setSpan(java.lang.Object, int, int, int)`. |