Sunday, March 13, 2011

Displaying a Web Page from Within an Android Activity

Android WebViews let you display fully formatted HTML inside of an Android Activity.This tutorial will walk you through adding and using a webview.

Android WebViews

If you have followed through a couple of the tutorials already, or have already made a couple of applications on your own, then you have no doubt used several of the built in View types available from Android. One view you may not have known about is the Android WebView.

AndroidDom loaded in a webview

Creating the Layout

For some unknown reason, the WebView does not show up on the list of available views from the Android layout editor, so to add the WebView we must edit the layout XML directly.

The file below represents a simple layout that features a WebView that takes up the entire space.


<?xml version="1.0" encoding="utf-8"?>
  android:layout_height="fill_parent" android:layout_width="fill_parent">

Adding the Internet Permission

In order for the WebView to download webpages from the internet, you will need to enable the internet permission for your application. To do this, edit the manifest file for the app.

Add the following line after the closing application tag, but before the closing manifest tag.

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

Loading a Website

Now that we have a webview and enabled the app to access the internet, we can download a webpage. The WebView is given the id "webview" so we can use that to get access to the view, and load a webpage from the activity.

Add the following import to the top of your java file.
import android.webkit.WebView;

Use the following code to load a web page
WebView webview = (WebView) findViewById(;

If you haven't properly added the Internet permission to your application, or you have entered a bad url you will get a message similar to the following.

WebView displaying an error

If the web page loads successfully, you should see actual content, like what is listed below.

Screenshot of AndroidDom loaded in the webview


Web Views are a great way to integrate more content into your Android apps and very easy to use and work with.

Full Source

The full source of is listed below. The full source for the layout is listed under the "Creating a Layout" Step earlier in the tutorial
package com.androiddom.webview;

import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        WebView webview = (WebView) findViewById(;


  1. I have tried this code, but it is displaying the same error on each url that "web page is not available", even though i have added the internet access permission in the manifest.
    kindly tell me the solution. really need it!

  2. I have tried this code but it is not working in my android app. Only my android layout display but in webview i am not able to load/open url. I have added internet permission in manifest file.
    So kindly tell me the solution, it is needed.
    Thanks in advance,

    1. Definately u guys have no internet connection...........

  3. This comment has been removed by the author.

  4. thank you,i tried it's working,but it does not occupy complete activity.
    lil bit space is remaining from all sides

  5. This comment has been removed by the author.

  6. how do i get it from not opening up the browser when tapping links?

  7. hello friends take a look of this link for more good examples.

  8. How do I display part of webpage in an activity

  9. Thanks.. its working for me.It displaying the home but its opening the links in web browser. Tellme how to open the links with in the app itself.