# PrecomputedTextCompat

Artifact: [androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core) [View Source](https://cs.android.com/search?q=file:androidx/core/text/PrecomputedTextCompat.java+class:androidx.core.text.PrecomputedTextCompat) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

*** ** * ** ***

<br />

```
class PrecomputedTextCompat : Spannable
```

<br />

*** ** * ** ***

A text which has the character metrics data. A text object that contains the character metrics data and can be used to improve the performance of text layout operations. When a PrecomputedTextCompat is created with a given `https://developer.android.com/reference/java/lang/CharSequence.html`, it will measure the text metrics during the creation. This PrecomputedText instance can be set on `https://developer.android.com/reference/android/widget/TextView.html` or `https://developer.android.com/reference/android/text/StaticLayout.html`. Since the text layout information will be included in this instance, `https://developer.android.com/reference/android/widget/TextView.html` or `https://developer.android.com/reference/android/text/StaticLayout.html` will not have to recalculate this information. On API 29 or later, there is full PrecomputedText support by framework. From API 23 to API 27, PrecomputedTextCompat relies on internal text layout cache. PrecomputedTextCompat immediately computes the text layout in the constructor to warm up the internal text layout cache. Note that any `https://developer.android.com/reference/android/text/NoCopySpan.html` attached to the original text won't be passed to PrecomputedText.

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params` The information required for building `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat`. |
| `class https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params.Builder` A builder for creating `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params`. |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#charAt(int)(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` |
| `java-static https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat!` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#create(java.lang.CharSequence,androidx.core.text.PrecomputedTextCompat.Params)(text: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html, params: https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params)` Create a new `https://developer.android.com/reference/android/text/PrecomputedText.html` which will pre-compute text measurement and glyph positioning information. |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getParagraphCount()()` Returns the count of paragraphs. |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getParagraphEnd(int)(paraIndex: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the paragraph end offset of the text. |
| `@https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getParagraphStart(int)(paraIndex: @https://developer.android.com/reference/kotlin/androidx/annotation/IntRange(from = 0) https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the paragraph start offset of the text. |
| `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getParams()()` Returns the parameters used to measure this text. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getSpanEnd(java.lang.Object)(tag: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getSpanFlags(java.lang.Object)(tag: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getSpanStart(java.lang.Object)(tag: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<T!>!` | `<T> https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getSpans(int,int,java.lang.Class<T>)(start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, end: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, type: https://developer.android.com/reference/java/lang/Class.html<T!>!)` //////////////////////////////////////////////////////////////////////////////////////////// |
| `java-static https://developer.android.com/reference/java/util/concurrent/Future.html<https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat!>!` | `@https://developer.android.com/reference/kotlin/androidx/annotation/UiThread https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#getTextFuture(java.lang.CharSequence,androidx.core.text.PrecomputedTextCompat.Params,java.util.concurrent.Executor)( charSequence: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html, params: https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params, executor: https://developer.android.com/reference/java/util/concurrent/Executor.html? )` Helper for PrecomputedText that returns a future to be used with `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView#setTextFuture(java.util.concurrent.Future<androidx.core.text.PrecomputedTextCompat>)`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#length()()` //////////////////////////////////////////////////////////////////////////////////////////// |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#nextSpanTransition(int,int,java.lang.Class)(start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, limit: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, type: https://developer.android.com/reference/java/lang/Class.html!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#removeSpan(java.lang.Object)(what: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#setSpan(java.lang.Object,int,int,int)(what: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!, start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, end: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, flags: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html!` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#subSequence(int,int)(start: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, end: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat#toString()()` |

### Inherited Constants

From [android.text.Spanned](https://developer.android.com/reference/android/text/Spanned.html) `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_COMPOSING = 256` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_EXCLUSIVE_EXCLUSIVE = 33` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_EXCLUSIVE_INCLUSIVE = 34` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_INCLUSIVE_EXCLUSIVE = 17` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_INCLUSIVE_INCLUSIVE = 18` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_INTERMEDIATE = 512` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_MARK_MARK = 17` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_MARK_POINT = 18` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_PARAGRAPH = 51` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_POINT_MARK = 33` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_POINT_MARK_MASK = 51` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_POINT_POINT = 34` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_PRIORITY = 16711680` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_PRIORITY_SHIFT = 16` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_USER = -16777216` `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://developer.android.com/reference/android/text/Spanned.html#SPAN_USER_SHIFT = 24`

### Inherited functions

From [java.lang.CharSequence](https://developer.android.com/reference/java/lang/CharSequence.html) `https://developer.android.com/reference/java/util/stream/IntStream.html!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/chars.html()` `https://developer.android.com/reference/java/util/stream/IntStream.html!` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/code-points.html()` `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/compare.html(cs1: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html!, cs2: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html!)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/get-chars.html(srcBegin: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, srcEnd: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, dst: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char/index.html>!, dstBegin: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/is-empty.html()`

## Public functions

### charAt

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun charAt(index: Int): Char
```

### create

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
java-static fun create(text: CharSequence, params: PrecomputedTextCompat.Params): PrecomputedTextCompat!
```

Create a new `https://developer.android.com/reference/android/text/PrecomputedText.html` which will pre-compute text measurement and glyph positioning information.

This can be expensive, so computing this on a background thread before your text will be presented can save work on the UI thread.
Note that any `https://developer.android.com/reference/android/text/NoCopySpan.html` attached to the text won't be passed to the created PrecomputedText.

| Parameters |
|---|---|
| `text: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html` | the text to be measured |
| `params: https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params` | parameters that define how text will be precomputed |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat!` | A `https://developer.android.com/reference/android/text/PrecomputedText.html` |

### getParagraphCount

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getParagraphCount(): @IntRange(from = 0) Int
```

Returns the count of paragraphs.

### getParagraphEnd

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getParagraphEnd(paraIndex: @IntRange(from = 0) Int): @IntRange(from = 0) Int
```

Returns the paragraph end offset of the text.

### getParagraphStart

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getParagraphStart(paraIndex: @IntRange(from = 0) Int): @IntRange(from = 0) Int
```

Returns the paragraph start offset of the text.

### getParams

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getParams(): PrecomputedTextCompat.Params
```

Returns the parameters used to measure this text.

### getSpanEnd

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getSpanEnd(tag: Any!): Int
```

### getSpanFlags

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getSpanFlags(tag: Any!): Int
```

### getSpanStart

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun getSpanStart(tag: Any!): Int
```

### getSpans

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun <T> getSpans(start: Int, end: Int, type: Class<T!>!): Array<T!>!
```

////////////////////////////////////////////////////////////////////////////////////////////

### getTextFuture

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
@UiThread
java-static fun getTextFuture(
    charSequence: CharSequence,
    params: PrecomputedTextCompat.Params,
    executor: Executor?
): Future<PrecomputedTextCompat!>!
```

Helper for PrecomputedText that returns a future to be used with `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView#setTextFuture(java.util.concurrent.Future<androidx.core.text.PrecomputedTextCompat>)`. PrecomputedText is suited to compute on a background thread, but when TextView properties are dynamic, it's common to configure text properties and text at the same time, when binding a View. For example, in a RecyclerView Adapter:

```kotlin
    void onBindViewHolder(ViewHolder vh, int position) {
        ItemData data = getData(position);

        vh.textView.setTextSize(...);
        vh.textView.setFontVariationSettings(...);
        vh.textView.setText(data.text);
    }
```
In such cases, using PrecomputedText is difficult, since it isn't safe to defer the setText() code arbitrarily - a layout pass may happen before computation finishes, and will be incorrect if the text isn't ready yet.

With `getTextFuture()`, you can block on the result of the precomputation safely before the result is needed. AppCompatTextView provides `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView#setTextFuture(java.util.concurrent.Future<androidx.core.text.PrecomputedTextCompat>)` for exactly this use case. With the following code, the app's layout work is largely done on a background thread:

```kotlin
    void onBindViewHolder(ViewHolder vh, int position) {
        ItemData data = getData(position);

        vh.textView.setTextSize(...);
        vh.textView.setFontVariationSettings(...);

        // start precompute
        Futurefuture = PrecomputedTextCompat.getTextFuture(
                data.text, vh.textView.getTextMetricsParamsCompat(), myExecutor);

        // and pass future to TextView, which awaits result before measuring
        vh.textView.setTextFuture(future);
    }
```
Because RecyclerView `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.LayoutManager#isItemPrefetchEnabled()` bind multiple frames in advance while scrolling, the text work generally has plenty of time to complete before measurement occurs.

**Note:** all TextView layout properties must be set before creating the Params object. If they are changed during the precomputation, this can cause a `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` when the precomputed value is consumed during measure, and doesn't reflect the TextView's current state.

| Parameters |
|---|---|
| `charSequence: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char-sequence/index.html` | the text to be displayed |
| `params: https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat.Params` | the parameters to be used for displaying text |
| `executor: https://developer.android.com/reference/java/util/concurrent/Executor.html?` | the executor to be process the text layout. If null is passed, the default single threaded pool will be used. |

| Returns |
|---|---|
| `https://developer.android.com/reference/java/util/concurrent/Future.html<https://developer.android.com/reference/kotlin/androidx/core/text/PrecomputedTextCompat!>!` | a future of the precomputed text |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView#setTextFuture(java.util.concurrent.Future<androidx.core.text.PrecomputedTextCompat>)` |   |

### length

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun length(): Int
```

////////////////////////////////////////////////////////////////////////////////////////////

### nextSpanTransition

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun nextSpanTransition(start: Int, limit: Int, type: Class!): Int
```

### removeSpan

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun removeSpan(what: Any!): Unit
```

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `https://developer.android.com/reference/android/text/style/MetricAffectingSpan.html` is specified. |

### setSpan

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun setSpan(what: Any!, start: Int, end: Int, flags: Int): Unit
```

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if `https://developer.android.com/reference/android/text/style/MetricAffectingSpan.html` is specified. |

### subSequence

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/core#1.1.0)

```
fun subSequence(start: Int, end: Int): CharSequence!
```

### toString

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