1

I use StaticLayout to paginate my text to create an epub application, and I have a text that has 16,000 lines.

The StaticLayout object is created about 6 seconds later and it's very slow.

How can I reduce this time or is there another way to use StaticLayout instead?

Ryan M
  • 18,333
  • 31
  • 67
  • 74

1 Answers1

1

The problem is that you're trying to lay out the whole thing at once. Laying out 16,000 lines of text is expensive. It's going to take a nontrivial amount of time even on a desktop computer with a powerful CPU, and you're trying to do it on a phone.

Instead, take a progressive approach. Take an approximation of the amount of text you expect might fit in one page - maybe 1000 characters. You can tweak this value. Lay that out, see if it's enough, then lay out more if needed.

You can use DynamicLayout to do this efficiently. Make sure to pass it an Editable when creating it to allow you to update the layout as you add more text.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • Can you give me an example? I am working on a project and its deadline is coming to an end. I need a practical example of what you said. My only request is to process a very long text for pagination and display. Thank you @Ryan M. – Arman Hosseini May 06 '20 at 16:56
  • Hi @ryan-m . Can you help me? – Arman Hosseini May 07 '20 at 09:56
  • Hi, I'm afraid I'm not sufficiently familiar with these APIs to write out a full example without quite a lot of work - it's been a while since I've used them myself. I hope that my answer points you in the right direction, though. For DynamicLayout specifically, you may want to look at the framework code that uses it via https://cs.android.com/ - you can easily search the entire Android codebase from there. Best of luck! – Ryan M May 07 '20 at 09:58
  • Hi @Ryan M. I used your solution but it still doesn't work. Should i use EditableTextView ? – Arman Hosseini May 19 '20 at 17:10
  • "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, describe what the exact observed behavior is and what the expected/intended behavior should be. For UI issues, a screenshot or video is usually helpful. Include the exact text of any error messages, including the full [stack trace](/a/23353174) of any exceptions, if applicable, as well as which line of code the stack trace points to. Please see [ask] and [How to create a Minimal, Reproducible Example](/help/minimal-reproducible-example). – Ryan M May 19 '20 at 20:13