# SpanUtil

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

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/common/text/SpanUtil "View this page in Java")

<br />

```
@UnstableApi
class SpanUtil
```

<br />

*** ** * ** ***

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

## Summary

|                                    ### Public functions                                     |
|---------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [addInheritedRelativeSizeSpan](https://developer.android.com/reference/kotlin/androidx/media3/common/text/SpanUtil#addInheritedRelativeSizeSpan(android.text.Spannable,float,int,int,int))`(` ` spannable: `[Spannable](https://developer.android.com/reference/android/text/Spannable.html)`!,` ` size: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`,` ` start: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` end: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` spanFlags: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Modifies the size of the text between`start`and`end`relative to any existing[RelativeSizeSpan](https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html)instances which cover**at least the same range**. |
| `java-static `[Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [addOrReplaceSpan](https://developer.android.com/reference/kotlin/androidx/media3/common/text/SpanUtil#addOrReplaceSpan(android.text.Spannable,java.lang.Object,int,int,int))`(` ` spannable: `[Spannable](https://developer.android.com/reference/android/text/Spannable.html)`!,` ` span: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`!,` ` start: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` end: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`,` ` spanFlags: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) `)` Adds`span`to`spannable`between`start`and`end`, removing any existing spans of the same type and with the same indices and flags.                                                                                                                  |

## Public functions

### addInheritedRelativeSizeSpan

```
java-static fun addInheritedRelativeSizeSpan(
    spannable: Spannable!,
    size: Float,
    start: Int,
    end: Int,
    spanFlags: Int
): Unit
```

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

[RelativeSizeSpan](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[RelativeSizeSpan](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[RelativeSizeSpan](https://developer.android.com/reference/android/text/style/RelativeSizeSpan.html)instances covering between`start`and`
end`.

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

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

### addOrReplaceSpan

```
java-static fun addOrReplaceSpan(
    spannable: Spannable!,
    span: Any!,
    start: Int,
    end: Int,
    spanFlags: Int
): Unit
```

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.[ForegroundColorSpan](https://developer.android.com/reference/android/text/style/ForegroundColorSpan.html).  

|                                            Parameters                                            |
|--------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|
| `spannable: `[Spannable](https://developer.android.com/reference/android/text/Spannable.html)`!` | The[Spannable](https://developer.android.com/reference/android/text/Spannable.html)to add`span`to.                                           |
| `span: `[Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)`!`           | The span object to be added.                                                                                                                 |
| `start: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)             | The start index to add the new span at.                                                                                                      |
| `end: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)               | The end index to add the new span at.                                                                                                        |
| `spanFlags: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)         | The flags to pass to[setSpan](https://developer.android.com/reference/android/text/Spannable.html#setSpan(java.lang.Object, int, int, int)). |