The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


The WebView Widget

HTML has come a long way from Sir Tim Berners-Lee’s original vision of using it to publish physics papers.

Not surprisingly, displaying HTML, CSS, and JavaScript in mobile applications is fairly popular, not only for creating full-fledged Web browsers, but for rendering HTML content from RSS/Atom feeds, from HTML-formatted email messages, ebooks (like the one you are reading), and so forth.

There are a couple of ways to display HTML in Android, with the most powerful being the WebView widget, the focus of this chapter.

Role of WebView

If your HTML is fairly limited in scope, such as what you might find in the body of a status update on Twitter, you can use the static fromHtml() method on the Html utility class to parse an HTML-formatted string into something that you can put into a TextView. TextView can render simple formatting like styles (bold, italic, etc.), font faces (serif, sans serif, etc.), colors, links, and so forth.

However, sometimes your needs for HTML transcend what TextView can handle. You will not be browsing Facebook using TextView, for example.

In those cases, WebView will be the more appropriate widget, as it can handle a much wider range of HTML tags. WebView can also handle CSS and JavaScript, which Html.fromHtml() would simply ignore. WebView can also assist you with common “browsing” metaphors, such as history list of visited URLs to support backwards and forwards navigation.

On the other hand, WebView is a much more expensive widget to use, in terms of memory consumption, than is TextView.

Daddy, Where Do WebViews Come From?

Originally, the story was simple: WebView was powered by a fairly complete copy of WebKit, the Web rendering engine behind Safari and, originally, Chrome.

In Android 4.4, Google switched rendering engines. Depending on who you asked, WebView was powered by Chromium or Blink. Chromium is an open source browser that forms the foundation for Google’s Chrome, and Blink is a fork of WebKit created by Opera and Google that, in turn, powers Chromium.

Starting in Android 5.0, the implementation of WebView was no longer a part of Android. Rather, it became a separate “System WebView” app, distributed through the Play Store. The idea was that this app could be updated independently of the device firmware, so that WebView bugs could be fixed more rapidly and distributed to more devices. This also means that Google can distribute new and exciting bugs more quickly (and independently of Android OS version), as will be discussed later in the chapter.

In Android 7.0, the implementation of WebView will be from one of two places:

The documented dependency of WebView on apps distributed through the Play Store makes things very murky for non-Play ecosystem devices, such as most devices in China. Most likely, individual manufacturers do their own thing with respect to updating WebView.

As a result, from the standpoint of security and compatibility, WebView is a “hot mess”.

Adding the Widget

The preview of this section was traded for a bag of magic beans.

Loading Content Via a URL

The preview of this section was lost due to a rupture in the space-time continuum.

Links and Redirects

The preview of this section was last seen in the Bermuda Triangle.

Supporting JavaScript

The preview of this section is out seeking fame and fortune as the Dread Pirate Roberts.

Alternatives for Loading Content

The preview of this section was lost in the sofa cushions.

Listening for Events

The preview of this section is sleeping in.

Addressing the Link/Redirect Behavior

The preview of this section was stepped on by Godzilla.

Visit the Trails!

The preview of this section was traded for a bag of magic beans.