Android WebView(Basic)

This is basic instruction of WebView in Android.
WebView is a helper view parts to show web page.
It is not powerful compare to browser, but easy to use and manage webpage.

Though no UI control to manage navigation, we can manage to some extent by code.

Create WebView in layout xml

This is simple WebView only layout xml.

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

Load web page

setContentView(R.layout.webtest);
WebView webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("http://www.rakuten.com.sg");

Enable JavaScript

Certainly, javascript has possibility to work bad, for example XSS.
But these days, it is inevitable not to use javascript.
To enable javascript, you need

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

Don’t care of warning.

Handle WebView Event

To handle webview event, you use WebViewClient
Set this instance to WebView

webView.setWebViewClient(new WebViewClient()
{

	@Override
	public void onPageFinished(WebView view, String url) 
	{
		super.onPageFinished(view, url);
	}

	@Override
	public void onReceivedError(WebView view, int errorCode,
			String description, String failingUrl) 
	{
		super.onReceivedError(view, errorCode, description, failingUrl);
	}

	@Override
	public boolean shouldOverrideUrlLoading(WebView view, String url) 
	{
		return super.shouldOverrideUrlLoading(view, url);
	}
			
});

Event Methods

Event methods Description
public void onPageFinished(WebView view, String url) Finished loading
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
Failed to load
public boolean shouldOverrideUrlLoading(WebView view, String url) When loading

Result

Android WebView

The methods you can use are Android Developer.

Go back, forward

WebView has methods for them.

webView.goBack();      // Back
webView.goForward();   // Forward

Can go back, forward or not

canGoBack(), canGoForward()

if ( webView.canGoBack() ) 
{
   webView.goBack();
}

if ( webView.canGoForward() ) 
{
   webView.goForward();
}

Ref

Android Developer
stackoverflow