How can I block EditText layout reflow when adding spans?

37 Views Asked by At

I am trying to batch-add several thousand spans to a SpannableStringBuilder in an EditText (obtained via getText()).

This is slower than I would like. Profiling has shown that the vast majority of the time is being spent in DynamicLayout.reflow. Is there a way to block the layout reflow until I am done adding spans?

1

There are 1 best solutions below

0
On

To batch text reflow / layout changes use AppCompatMultiAutoCompleteTextView as your base class and surround the code you wish to batch with beginBatchEdit() and endBatchEdit()

i.e.


public class MyEditText extends AppCompatMultiAutoCompleteTextView {

    private void batchUpdateSpans() {
        try {
            beginBatchEdit();
            updateSpans();
        } finally {
            endBatchEdit();
        } 
    }

}

AppCompatMultiAutoCompleteTextView uses SpannableBuilder, a subclass of SpannableStringBuilder with batching support.