getTextFuture

Helper for PrecomputedText that returns a future to be used with 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 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
        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 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.

Return

a future of the precomputed text

Parameters

charSequence

the text to be displayed

params

the parameters to be used for displaying text

executor

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

See also

androidx.appcompat.widget.AppCompatTextView#setTextFuture