Android WebView example

WebView is powerful component in Android. It allows you to load a web page on the internet in your Android application independently from a web browser. It is able to display static and dynamic web pages. By default, the WebView doesn't allow JavaScript. To enable JavaScript in the WebView, you need to call the WebSettings. To load a web page in the WebView, you pass the string url of the web page to the loadUrl() method. Note that in order your application to use the Internet and load web pages in the WebView, you must add the INTERNET permissions to your Android Manifest file:

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

Now to have an example application on using the WebView to display a web page, create a new Android project. Then add a WebView to the activity_main.xml file.

<RelativeLayout xmlns:android=""
   tools:context=".MainActivity" >



in the onCreate() method of the MainActivity class, you will write code to get a reference to the WebView, enable JavaScript, and load a web page as shown below.

package com.example.webviewer;

import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
WebView wview=(WebView)findViewById(;
//enable JavaScript
WebSettings settings=wview.getSettings();
//prevent a browser pop up
wview.setWebViewClient(new WebViewClient());
//display the web page on the WebView wview.loadUrl("");


public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;



Posted by: Dara | post date: 08-22-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.