The Android Developer Challenge is back! Submit your idea before December 2.

PrecomputedTextCompat

open class PrecomputedTextCompat : Spannable
kotlin.Any
   ↳ androidx.core.text.PrecomputedTextCompat

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 CharSequence, it will measure the text metrics during the creation. This PrecomputedText instance can be set on android.widget.TextView or StaticLayout. Since the text layout information will be included in this instance, android.widget.TextView or StaticLayout will not have to recalculate this information. On API 29 or later, there is full PrecomputedText support by framework. From API 21 to API 27, PrecomputedTextCompat relies on internal text layout cache. PrecomputedTextCompat immediately computes the text layout in the constuctor to warm up the internal text layout cache. On API 20 or before, PrecomputedTextCompat does nothing. Note that any android.text.NoCopySpan attached to the original text won't be passed to PrecomputedText.

Summary

Nested classes

The information required for building PrecomputedTextCompat.

Public methods

open static PrecomputedTextCompat!
create(@NonNull text: CharSequence, @NonNull params: PrecomputedTextCompat.Params)

Create a new PrecomputedText which will pre-compute text measurement and glyph positioning information.

open Char
get(index: Int)

open Int

Returns the count of paragraphs.

open Int
getParagraphEnd(paraIndex: Int)

Returns the paragraph end offset of the text.

open Int
getParagraphStart(paraIndex: Int)

Returns the paragraph start offset of the text.

open PrecomputedTextCompat.Params

Returns the parameters used to measure this text.

open Int
getSpanEnd(tag: Any!)

open Int

open Int

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

open static Future<PrecomputedTextCompat!>!
getTextFuture(@NonNull charSequence: CharSequence, @NonNull params: PrecomputedTextCompat.Params, @Nullable executor: Executor?)

Helper for PrecomputedText that returns a future to be used with androidx.appcompat.widget.AppCompatTextView#setTextFuture.

open Int
nextSpanTransition(start: Int, limit: Int, type: Class<Any!>!)

open Unit
removeSpan(what: Any!)

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

open CharSequence
subSequence(startIndex: Int, endIndex: Int)

open String

Properties

open Int

Public methods

create

open static fun create(@NonNull text: CharSequence, @NonNull params: PrecomputedTextCompat.Params): PrecomputedTextCompat!

Create a new PrecomputedText 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 android.text.NoCopySpan attached to the text won't be passed to the created PrecomputedText.
Parameters
text CharSequence: the text to be measured
params CharSequence: parameters that define how text will be precomputed
Return
PrecomputedTextCompat!: A PrecomputedText

get

open fun get(index: Int): Char

getParagraphCount

open fun getParagraphCount(): Int

Returns the count of paragraphs.

getParagraphEnd

open fun getParagraphEnd(paraIndex: Int): Int

Returns the paragraph end offset of the text.

getParagraphStart

open fun getParagraphStart(paraIndex: Int): Int

Returns the paragraph start offset of the text.

getParams

@NonNull open fun getParams(): PrecomputedTextCompat.Params

Returns the parameters used to measure this text.

getSpanEnd

open fun getSpanEnd(tag: Any!): Int

getSpanFlags

open fun getSpanFlags(tag: Any!): Int

getSpanStart

open fun getSpanStart(tag: Any!): Int

getSpans

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

getTextFuture

@UiThread open static fun getTextFuture(@NonNull charSequence: CharSequence, @NonNull params: PrecomputedTextCompat.Params, @Nullable executor: Executor?): Future<PrecomputedTextCompat!>!

Helper for PrecomputedText that returns a future to be used with androidx.appcompat.widget.AppCompatTextView#setTextFuture. 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:

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 androidx.appcompat.widget.AppCompatTextView#setTextFuture for exactly this use case. With the following code, the app's layout work is largely done on a background thread:

void onBindViewHolder(ViewHolder vh, int position) {
              ItemData data = getData(position);
     
              vh.textView.setTextSize(...);
              vh.textView.setFontVariationSettings(...);
     
              // start precompute
              Future<precomputedtextcompat>
  future = PrecomputedTextCompat.getTextFuture(
                      data.text, vh.textView.getTextMetricsParamsCompat(), myExecutor);
     
              // and pass future to TextView, which awaits result before measuring
              vh.textView.setTextFuture(future);
          }
      
</precomputedtextcompat>
Because RecyclerView prefetches 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 IllegalArgumentException when the precomputed value is consumed during measure, and doesn't reflect the TextView's current state.

Parameters
charSequence CharSequence: the text to be displayed
params CharSequence: the parameters to be used for displaying text
executor CharSequence: the executor to be process the text layout. If null is passed, the default single threaded pool will be used.
Return
Future<PrecomputedTextCompat!>!: a future of the precomputed text

nextSpanTransition

open fun nextSpanTransition(start: Int, limit: Int, type: Class<Any!>!): Int

removeSpan

open fun removeSpan(what: Any!): Unit
Exceptions
IllegalArgumentException if MetricAffectingSpan is specified.

setSpan

open fun setSpan(what: Any!, start: Int, end: Int, flags: Int): Unit
Exceptions
IllegalArgumentException if MetricAffectingSpan is specified.

subSequence

open fun subSequence(startIndex: Int, endIndex: Int): CharSequence

toString

@NonNull open fun toString(): String

Properties

length

open val length: Int