Home > Android > Android – WebView

Android – WebView

WebView is a view that display web pages inside your application. You can also specify HTML string and can show it inside your application using WebView. WebView makes turns your
application to a web application.

In order to add WebView to your application, you have to add element to your xml layout file. Its syntax is as follows –

<webview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"></webview>

In order to use it, you have to get a reference of this view in Java file. To get a reference, create an object of the class WebView. Its syntax is –

WebView browser = (WebView) findViewById(R.id.webview);

In order to load a web url into the WebView, you need to call a method loadUrl(String url) of the WebView class, specifying the required url. Its syntax is:


If you click on any link inside the webpage of the WebView , that page will not be loaded inside your WebView. In order to do that you need to extend your class from WebViewClient and override its method. Its syntax is –

private class MyBrowser extends WebViewClient {
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return true;

Source Code

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends Activity  {
   Button b1;
   EditText ed1;
   private WebView wv1;
   protected void onCreate(Bundle savedInstanceState) {
      wv1.setWebViewClient(new MyBrowser());
            String url = "www.google.com";
   private class MyBrowser extends WebViewClient {
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         return true;


<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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   <webview android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_alignparentbottom="true">