added in version 1.0.0
belongs to Maven artifact androidx.core:core:1.0.0-beta01

PrecomputedTextCompat

public class PrecomputedTextCompat
extends Object implements Spannable

java.lang.Object
   ↳ 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 TextView or StaticLayout. Since the text layout information will be included in this instance, TextView or StaticLayout will not have to recalculate this information. On API 28 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 NoCopySpan attached to the original text won't be passed to PrecomputedText.

Summary

Nested classes

class PrecomputedTextCompat.Params

The information required for building PrecomputedTextCompat

Inherited constants

Public methods

char charAt(int index)
static PrecomputedTextCompat create(CharSequence text, PrecomputedTextCompat.Params params)

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

int getParagraphCount()

Returns the count of paragraphs.

int getParagraphEnd(int paraIndex)

Returns the paragraph end offset of the text.

int getParagraphStart(int paraIndex)

Returns the paragraph start offset of the text.

PrecomputedTextCompat.Params getParams()

Returns the parameters used to measure this text.

int getSpanEnd(Object tag)
int getSpanFlags(Object tag)
int getSpanStart(Object tag)
<T> T[] getSpans(int start, int end, Class<T> type)
static Future<PrecomputedTextCompat> getTextFuture(CharSequence charSequence, PrecomputedTextCompat.Params params, Executor executor)

Helper for PrecomputedText that returns a future to be used with AppCompatTextView.setTextFuture(Future).

int length()
int nextSpanTransition(int start, int limit, Class type)
void removeSpan(Object what)
void setSpan(Object what, int start, int end, int flags)
CharSequence subSequence(int start, int end)
String toString()

Inherited methods

Public methods

charAt

added in version 1.0.0
public char charAt (int index)

Parameters
index int

Returns
char

create

added in version 1.0.0
public static PrecomputedTextCompat create (CharSequence text, 
                PrecomputedTextCompat.Params params)

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 NoCopySpan attached to the text won't be passed to the created PrecomputedText.

Parameters
text CharSequence: the text to be measured

params PrecomputedTextCompat.Params: parameters that define how text will be precomputed

Returns
PrecomputedTextCompat A PrecomputedText

getParagraphCount

added in version 1.0.0
public int getParagraphCount ()

Returns the count of paragraphs.

Returns
int

getParagraphEnd

added in version 1.0.0
public int getParagraphEnd (int paraIndex)

Returns the paragraph end offset of the text.

Parameters
paraIndex int

Returns
int

getParagraphStart

added in version 1.0.0
public int getParagraphStart (int paraIndex)

Returns the paragraph start offset of the text.

Parameters
paraIndex int

Returns
int

getParams

added in version 1.0.0
public PrecomputedTextCompat.Params getParams ()

Returns the parameters used to measure this text.

Returns
PrecomputedTextCompat.Params

getSpanEnd

added in version 1.0.0
public int getSpanEnd (Object tag)

Parameters
tag Object

Returns
int

getSpanFlags

added in version 1.0.0
public int getSpanFlags (Object tag)

Parameters
tag Object

Returns
int

getSpanStart

added in version 1.0.0
public int getSpanStart (Object tag)

Parameters
tag Object

Returns
int

getSpans

added in version 1.0.0
public T[] getSpans (int start, 
                int end, 
                Class<T> type)

Parameters
start int

end int

type Class

Returns
T[]

getTextFuture

added in version 1.0.0
public static Future<PrecomputedTextCompat> getTextFuture (CharSequence charSequence, 
                PrecomputedTextCompat.Params params, 
                Executor executor)

Helper for PrecomputedText that returns a future to be used with AppCompatTextView.setTextFuture(Future). 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 AppCompatTextView.setTextFuture(Future) 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 future = PrecomputedTextCompat.getTextFuture(
                 data.text, vh.textView.getTextMetricsParamsCompat(), myExecutor);

         // and pass future to TextView, which awaits result before measuring
         vh.textView.setTextFuture(future);
     }
 
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 PrecomputedTextCompat.Params: the parameters to be used for displaying text

executor Executor: the executor to be process the text layout. If null is passed, the default single threaded pool will be used.

Returns
Future<PrecomputedTextCompat> a future of the precomputed text

length

added in version 1.0.0
public int length ()

Returns
int

nextSpanTransition

added in version 1.0.0
public int nextSpanTransition (int start, 
                int limit, 
                Class type)

Parameters
start int

limit int

type Class

Returns
int

removeSpan

added in version 1.0.0
public void removeSpan (Object what)

Parameters
what Object

Throws
IllegalArgumentException if MetricAffectingSpan is specified.

setSpan

added in version 1.0.0
public void setSpan (Object what, 
                int start, 
                int end, 
                int flags)

Parameters
what Object

start int

end int

flags int

Throws
IllegalArgumentException if MetricAffectingSpan is specified.

subSequence

added in version 1.0.0
public CharSequence subSequence (int start, 
                int end)

Parameters
start int

end int

Returns
CharSequence

toString

public String toString ()

Returns
String