Android Paged TextView — an Idea
A while ago, playing around in Feedly, I wondered how difficult would have been to have an implementation where you can swipe to get to the next page, like you would do in an ebook application. Or, like you do in Google Currents, only it’s woefully inadequate as an RSS reader.
At first, I’ve tried to find an RSS reader that does this, but couldn’t find one. A friend challenged me to do it myself and well, why not? It should be pretty simple, right?
Apparently not. Predictably, I first headed for Stackoverflow. A few searches didn’t turn up much. The only thing close to a solution was a half-baked algorithm that added characters one by one to a text view until it was full. I didn’t even try to test it, but it must have been really slow.
Looking through the source code of two open source ebook reader apps, FBReader and CoolReader, I found complex, custom implementations of a text renderer, using native code. FBReader uses zlibrary, a library that allows cross-platform compilation (that’s all I could find about it) and the code is way too complex. CoolReader is also cross platform, and uses native code and crengine, their rendering engine for XML, CSS and stuff.
Out of ideas, I ventured into the source code of TextView, assuming that I could find something in the code used to set the maximum line number. The thing is that TextView works backwards from what I needed. Given a chunk of text, it figures out how big the TextView must be to fit it. If the sizes are fixed, then it just displays what can be displayed. All this happens using the android.text.Layout subclasses: BoringLayout, StaticLayout and DynamicLayout.
All I wanted was a simple and clean way to do what I wanted. I headed for Reddit and Google+. I got some answers on Reddit, suggesting either a native implementation, or a more hackish use of StaticLayout.
As I was already starting to think about how to use StaticLayout, I stumbled upon another open source ebook reader: PageTurner. A quick look through it’s source code (I was at work), gave me hope. There was a BookView, extending a ScrollView, and somewhere inside it was a TextView.
I couldn’t wait to get home to dig into it. Indeed, it was doing the exact thing I wanted to do: using a StaticLayout to calculate how much text can fit into a given TextView. The method used is brutal: take all the text and dump it in the TextView, then go line by line to see where the visible text starts and ends. But it worked, because it was only doing this once, and storing all the page start positions in the text.
The code is interwoven with the code for the actual book, and I’d like to build a simple and clean PagedTextView that can be used without having to know what happens inside.
Originally published at corneliudascalu.com.