Android WebView

Android WebView Tutorial and Examples.

A WebView is a view that displays web pages.

In Android it is represented by the WebView class. It is this class that is the basis upon which you can create your own web browser or simply display some online content within your Activity. WebView makes use of the WebKit rendering engine to display web pages and includes methods t:

  1. navigate forward and backward through a history
  2. zoom in and out.
  3. perform text searches and more.

WebView is very powerful as it provides you with a way to write applications in languages like Javascript and the HTML markup. There are so many frameworks that make use of this capability, thus allowing you write your app in HTML5 technologies. You can even turn your website, like say a wordpress website into an android app.

WebView API Definition

WebView is a concrete class residing in the android.webkit package. It derives from the android.widget.AbsoluteLayout class and implements several interfaces as shown below:

public class WebView extends AbsoluteLayout implements ViewTreeObserver.OnGlobalFocusChangeListener, ViewGroup.OnHierarchyChangeListener

Here's it's inheritance hierarchy:

   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.AbsoluteLayout
               ↳    android.webkit.WebView

Using WebView

Most of the time you will want to render online content in your webview. So in order for your Activity to access the Internet and load web pages in a WebView, you must add the INTERNET permissions to your Android Manifest file:

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

Then in your layout add a <WebView> in your layout, or set the entire Activity window as a WebView during onCreate():

 WebView webview = new WebView(this);

Once you've done that then you can load you webpage via the loadUrl() method:

 // Simplest usage: note that an exception will NOT be thrown
 // if there is an error loading this page (see below).

You can also load from an HTML string:

 String summary = "<html><body>You scored <b>192</b> points.</body></html>";
 webview.loadData(summary, "text/html", null);
 // ... although note that there are restrictions on what this HTML can do.
 // See the JavaDocs for loadData() and loadDataWithBaseURL() for more info. 

Quick WebView Examples and HowTo's

1. Commonly used WebView settings

Here are some comminly used WebView settings.

    // Commonly used WebViewSetting
    public static void initWebSetting(WebView webView) {
        WebSettings setting = webView.getSettings();

2. How to Create a Custom WebView

We want to create a custom webview that can be used in a NestedScrollView.

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;

public class MyWebView extends WebView {

    public MyWebView(Context context) {

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);

    public MyWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    public boolean onTouchEvent(MotionEvent event) {

        //Check pointer index to avoid -1 (error)
        if (MotionEventCompat.findPointerIndex(event, 0) == -1) {
            return super.onTouchEvent(event);

        if (event.getPointerCount() >= 1) {
        } else {

        return super.onTouchEvent(event);

    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);

Top Android WebView Examples

Let's look at some examples.

WebView is actually one of those classes that have existed in android since the beginning.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post