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?

The preview of this section is en route to Mars.

Adding the Widget

The preview of this section will not appear here for a while, due to a time machine mishap.

Loading Content Via a URL

The preview of this section is [REDACTED].

Links and Redirects

The preview of this section was abducted by space aliens.

Supporting JavaScript

The preview of this section will not appear here for a while, due to a time machine mishap.

Alternatives for Loading Content

The preview of this section was whisked away by a shark-infested tornado.

Listening for Events

The preview of this section is en route to Mars.

Addressing the Link/Redirect Behavior

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

Opting Out of Google Monitoring

The preview of this section took that left turn at Albuquerque.

Visit the Trails!

The preview of this section was abducted by space aliens.