WebViewClient Subclass - Content Rendering Callbacks

This section provides a tutorial example on how to implement a subclass of android.webkit.WebViewClient with callback methods to interface with the rendering process of the WebView content.

In the next release of my AndroidWeb, I want to play with the android.webkit.WebViewClient class, which allows you to implement some callback methods to interface with the rendering process of the WebView content.

Key callback methods in android.webkit.WebViewClient class are:

To use WebViewClient, you need to:

Here is how I implemented my subclass of WebViewClient to write log entries to see how Web pages are loaded to WebView:

/* AndroidWeb.java
 * Version 3.0 - Adding a WebViewClient
 * Copyright (c) 2012, HerongYang.com, All Rights Reserved.
package com.herongyang.web;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
import java.io.*;
import java.util.*;
public class AndroidWeb extends Activity {
   public void onCreate(Bundle savedInstanceState) {

      WebView view = new WebView(this);
      AndroidWebClient client = new AndroidWebClient();
   private class AndroidWebClient extends WebViewClient {
      public void onPageStarted(WebView view, String url,
         android.graphics.Bitmap favicon) {
         logLine(view, "onPageStarted() called: url = "+url);
      public void onPageFinished(WebView view, String url) {
         logLine(view, "onPageFinished() called: url = "+url);
      public void onLoadResource(WebView view, String url) {
         logLine(view, "onLoadResource() called: url = "+url);
      public void logLine(WebView view, String msg) {
         try {
            FileOutputStream fos =
               .openFileOutput("Activity.log", Context.MODE_APPEND);
            OutputStreamWriter out = new OutputStreamWriter(fos);
            out.write((new Date()).toString()+": "+msg+"\n");
         } catch (Exception e) {

I also increased version values in AndroidManifest.xml. android:versionCode="20150301" must be an integer acting like a build number. android:versionName="3.0" is the string representing version of the application.

<?xml version="1.0" encoding="utf-8"?>
<!-- AndroidWeb_AndroidManifest.xml
 - Version 3.0 - Adding INTERNET permission
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   <application android:label="@string/app_name"
      <activity android:name="AndroidWeb"
            <action android:name="android.intent.action.MAIN" />
               android:name="android.intent.category.LAUNCHER" />
   <uses-permission android:name="android.permission.INTERNET" />

Rebuild the debug package, reinstall it to the emulator and run it. Google search home page is displayed again.

Copy the log file out of the emulator and view it:

C:\herong>\local\android-sdk-windows\platform-tools\adb \
   pull /data/data/com.herongyang.web/files/Activity.log Activity.log

C:\herong\AndroidWeb>type Activity.log
20:17:24 onPageStarted() called: url = http://www.google.com/
20:17:24 onLoadResource() called: url = http://www.google.com/
20:17:25 onPageStarted() called: url = https://www.google.com/?gws_...
20:17:27 onLoadResource() called: url = https://ssl.gstatic.com/gb/...
20:17:28 onLoadResource() called: url = https://ssl.gstatic.com/gb/...
20:17:29 onLoadResource() called: url = https://www.google.com/imag...
20:17:30 onPageFinished() called: url = https://www.google.com/?gws...

My WebViewClient subclass works! The log file tells me that:

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 AboutAndroid - Application to Retrieve System Information

 android.app.Activity Class and Activity Lifecycle

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

Building Your Own Web Browser

 android.webkit.WebView - Web Browser Base Class

 AndroidWeb - My Own Web Browser

WebViewClient Subclass - Content Rendering Callbacks

 saveWebArchive() Method - Saving Web Archive Files

 Web Archive File - XML File of Base64 Encoded Data

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Archived Tutorials


 Full Version in PDF/EPUB