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.

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.

11 comments:

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!

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