I need to set a very long string ( an edge case) in a TextView that is -- only -- on a single line. The user case is this: the user must be able to select the whole string to copy it to their clipboard (if the devices' clipboard allows it). The issue is this: Whenever the strings gets super big, the UI starts to hang. It does so incrementally, and by that I mean, at 13,000 characters, it'll be fine. At 65000 characters, the UI load time is ~156 seconds.
From running the profiler, I saw that most of the "heavy lifting" is done at this method nComputeLineBreaks()
which is essentially a native method to calculate the number of linebreaks for the paragraph.
Also there's not much going on in my code, I'm just doing textview.text = text
.
Possible Solutions tried:
I saw this new API offered by google from API 28 that was (somewhat) compatible with API 21. I'm referring to PrecomputeText and I was attempting to follow this official android blog to try to improve performance but it was in vain
I tried to create a new textview in a coroutine to allow it to compute what it needed, then remove my previous view and attach the new one. Still created heavy lag
I did set a
maxLines
hoping it would reduce the computation of line breaks, but it failed
Also, here are some relevant post I found that didn't help much in my case:
Using a webview was one of the answers. However, in my case, a horizontal textview that can only display in one line, webview seems irrelevant
Essentially, from what I gathered, the setText call is the culprit. Unfortunately, that means manipulating the UI and, from my understanding, that is only achievalbe on the main thread, which would lead to dropped frames.
Any ideas would be more than welcome!