DroidCon NYC 2015 and Rendering HTML

droidconnycThe Droidcon NYC 2015 video presentations have become available offering 48 sessions covering a wide range of topics. What I like about this and other Droidcons is that presenters come with no hidden agenda. There’s usually very little promoting of their respective companies or products and instead it’s all about advice gained through experience. It’s also great to compare how others have solved problems that you have experienced yourself.

For me, one such problem is described in the Beautiful Typography on Android (or close enough) session presented by Lisa Wray of Genius. On multiple projects I have had the problem of having to render server sourced, sometimes human created, HTML in Android. As Lisa explains, there are two ways to do this. The first way is to use a WebView and the other is to use a TextView with fromHtml() which converts the HTML to lots of Spanned text.

The problem with the WebView is that is that it’s very inefficient and hence very slow if you have lots of pieces of html in a view or even worse, in a ListView or RecyclerView of views containing HTML. Basically, it’s impractical to mix WebViews with native components to create a complex view. Even if your view is just one WebView, the base styles and scroll performance end up being those dictated by the WebView. All these concerns are before we even think about how different WebView implementations render things differently, how to control when embedded images are fetched, when/where css/javascript comes from and the security implications of using a WebView.

The problem with TextView.fromHtml() is that the result is often incomplete and lacks the fidelity of the original HTML. Lisa Wray’s presentation shows how to modify the Spanned text created by fromHtml() to make it, as she says, “Beautiful (or close enough)”.

Stepping back, this situation pinpoints something that should probably be avoided at the architecture stage anyway – the app displaying arbitrary HTML from the server. Managing this so that it renders well in an app and across mobile OS platforms can be very difficult. This is because you are trying to mix web and app display paradigms. While HTML might look like being convenient, it’s not usually well suited to consistent display in apps. If you can, it’s better to store and communicate content as text and meta data.