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="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <WebView
     android:id="@+id/wview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
/>

</RelativeLayout>


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.app.Activity;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wview=(WebView)findViewById(R.id.wview);
//enable JavaScript
WebSettings settings=wview.getSettings();
settings.setJavaScriptEnabled(true);
//prevent a browser pop up
wview.setWebViewClient(new WebViewClient());
//display the web page on the WebView wview.loadUrl("http://www.worldbestlearningcenter.com/index.htm");

}

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

}




Android-WebView-example

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.