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.


Main.xml

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


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(R.id.webview);
webview.loadUrl("http://www.androiddom.com");

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

Conclusion

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 MainActivity.java is listed below. The full source for the layout is listed under the "Creating a Layout" Step earlier in the tutorial

MainActivity.java
package com.androiddom.webview;

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        WebView webview = (WebView) findViewById(R.id.webview);
        webview.loadUrl("http://www.androiddom.com");
    }

11 comments:

  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!

    ReplyDelete
  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,
    sumit.tyagi@kmgin.com

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

      Delete
  3. This comment has been removed by the author.

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

    ReplyDelete
  5. This comment has been removed by the author.

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

    ReplyDelete
  7. hello friends take a look of this link for more good examples.
    http://androidexample.com/Show_Loader_To_Open_Url_In_WebView__-_Android_Example/index.php?view=article_discription&aid=125&aaid=145

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

    ReplyDelete
  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.

    ReplyDelete