1

I’m creating an application which shows the text in TextView. Since the text might be very large it is nested in ScrollView.

Everything works fine until the text is really large - for example the content of the whole book. For such large content the application’s UI starts to be unresponsive because of the big memory requirements.

Solution 1 Simply split the book’s content and show it in multiple pages, e.g. by using ViewPager2.

Yes, this would definitely work. However the “infinite” vertical scrolling of the text is in my situation the UI design decision, therefore this solution is not an option for me.

Solution 2 Use RecyclerView and render each line of the “book” as separate TextView instance.

This would be great solution, but unfortunately I need to support text selection. This solution would prevent the user to select multiline text.

Question Could you suggest any solution which would allow “infinite“ (RecyclerView-like) scrolling of the text while preserving all the text selection and text formatting capabilities?

Michal Vician
  • 2,486
  • 2
  • 28
  • 47
  • 1
    That's a question that has bothered me a lot in the past. Unfortunately, there's no way that I know of. You would basically have to reimplement all selection behavior by yourself, and that won't be easy. There's a few question related to this but with EditText: [Android EditText alternative](https://stackoverflow.com/questions/29827029), [EditText Performance: Understanding GPU Process Time on Profile GPU Rendering](https://stackoverflow.com/questions/29801232) – Nicolas May 17 '20 at 21:51
  • 1
    I saw an ebook reader that combined solution 1 and 2, a ViewPager with each page having a ScrollView, content is split by chapter. That could be another solution. – Nicolas May 17 '20 at 21:52

1 Answers1

-1

RecyclerView is your best option:

  • I know that you mentioned it in your question, but you are approaching it in a wrong way, because you said you want to render every line of the book as a seperate TextView item (which is not the way to go about it).

Instead:

1) The item of the recyclerView will be some LinearLayout containing a TextView (it will be like the page item).

2) Assuming the book is one big String, split it into chunks of substrings(in a way that the substring is like a page), and put these chunks of substrings in some list:

List<String> chunksList = new ArrayList<String>();

3) You later pass this list to the adapter, and render each substring according to position.

   adapter = new bookAdapter(........., chunksList);

4) It doesn't matter how big the book String is, you can scroll dynamically. Because its up to the chunksList and how many substrings it contains (keep in mind the chunksList will be different for each book that you want to render).

5) Also using this is approach you can use pagination.

GoodLuck.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22
  • I see, this would be a bit better solution from performance point of view. But it isn’t really so different from **solution 2** and would still prevent the user to select the text across pages/chunks. And that’s the required feature in my case. – Michal Vician May 18 '20 at 07:05