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 being chased by zombies.

Adding the Widget

The preview of this section did not survive Thanos's finger snap.

Loading Content Via a URL

The preview of this section is sleeping in.

Links and Redirects

The preview of this section left for Hollywood to appear in a reality TV show.

Supporting JavaScript

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

Alternatives for Loading Content

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

Listening for Events

The preview of this section did not survive Thanos's finger snap.

Addressing the Link/Redirect Behavior

The preview of this section is sleeping in.

Opting Out of Google Monitoring

The preview of this section is in an invisible, microscopic font.

Visit the Trails!

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