Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Hello, Views >

Hello, WebView

A WebView allows you to create your own web browser Activity. In this tutorial, we'll create a simple Activity that can view web pages.

  1. Create a new project/Activity called HelloWebView.
  2. Open the layout file. Insert a WebView so it looks like so:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android=""
  3. Now open the file. At the top of the class, instantiate a WebView object:
    WebView webview;

    Then add the following at the end of the onCreate() method:

    webview = (WebView) findViewById(;

    This captures the WebView we created in our layout, then requests a WebSettings object and enables JavaScript. Then we load a URL.

  4. Because we're accessing the internet, we need to add the appropriate permissions to the Android manifest file. So open the AndroidManifest.xml file and, add the following as a child of the <manifest> element:
    <uses-permission android:name="android.permission.INTERNET" />
  5. Now run it.

You now have the world's simplest web page viewer. It's not quite a browser yet. It only loads the page we've requested.

We can load a page, but as soon as we click a link, the default Android web browser handles the Intent, instead of our own WebView handling the action. So now we'll override the WebViewClient to enable us to handle our own URL loading.

  1. In the HelloAndroid Activity, add this nested private class:
    private class HelloWebViewClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
  2. Now, in the onCreate() method, set an instance of the HelloWebViewClient as our WebViewClient:
    webview.setWebViewClient(new WebViewClientDemo());

    This line should immediately follow the initialization of our WebView object.

    What we've done is create a WebViewClient that will load any URL selected in our WebView in the same WebView. You can see this in the shouldOverrideUrlLoading() method, above—it is passed the current WebView and the URL, so all we do is load the URL in the given view. Returning true says that we've handled the URL ourselves and the event should not bubble-up.

    If you try it again, new pages will now load in the HelloWebView Activity. However, you'll notice that we can't navigate back. We need to handle the back button on the device, so that it will return to the previous page, rather than exit the application.

  3. To handle the back button key press, add the following method inside the HelloWebView Activity:
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            return true;
        return super.onKeyDown(keyCode, event);

    The condition uses a KeyEvent to check whether the key pressed is the BACK button and whether the WebView is actually capable of navigating back (if it has a history). If both are not true, then we send the event up the chain (and the Activity will close). But if both are true, then we call goBack(), which will navigate back one step in the history. We then return true to indicate that we've handled the event.

When you open the application, it should look like this:


← Back to Hello, Views

  Published under the terms fo the Apache 2.0 License Design by Interspire