Android UI How to - extends WebViewClient








The following code shows how to extends WebViewClient.

Example

Add permission to manifest file

<manifest 
    ...
  <uses-permission android:name="android.permission.INTERNET" />
  <application>
    <activity 
         ...
    </activity>
  </application>
</manifest>

Layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <WebView android:id="@+id/webkit"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
  />
</LinearLayout>

Java code

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Date;
// www . ja  va  2s.  c  o  m
public class MainActivity extends Activity {
  WebView browser;
  
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    browser=(WebView)findViewById(R.id.webkit);
    browser.setWebViewClient(new Callback());
    
    loadTime();
  }
  
  void loadTime() {
    String page="<html><body><a href=\"clock\">"
            +new Date().toString()
            +"</a></body></html>";
    browser.loadDataWithBaseURL("x-data://base", page,"text/html", "UTF-8",null);
  }

  private class Callback extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
      loadTime();
      
      return(true);
    }
  }
}
null