How to use WebView in Android

public class WebView 
extends AbsoluteLayout 
implements 
    ViewTreeObserver.OnGlobalFocusChangeListener, 
    ViewGroup.OnHierarchyChangeListener

WebView is a view that display web pages inside the application. It is used to turn the application into a web application.

Class Hierarchy :

java.lang.Object
   ↳  android.view.View
        ↳  android.view.ViewGroup
             ↳  android.widget.AbsoluteLayout
                  ↳  android.webkit.WebView

MainActivity.java



filter_none

edit
close

play_arrow

link
brightness_4
code

package com.example.hp.webview;
  
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          
        // Binding MainActivity.java with 
        // activity_main.xml file
        setContentView(R.layout.activity_main);
          
        // Find the WebView by its unique ID
        WebView w = (WebView) findViewById(R.id.web);
          
        // loading http://www.google.com url in the the WebView.
        w.loadUrl("http://www.google.com");
          
        // this will enable the javascipt.
        w.getSettings().setJavaScriptEnabled(true);
          
        // WebViewClient allows you to handle 
        // onPageFinished and override Url loading.
        w.setWebViewClient(new WebViewClient());
          
    }
}

chevron_right


 
activity_main.xml

In the xml file only use of WebView is made inside RelativeLayout.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hp.webview.MainActivity">
  
    <WebView
        <!-- covers 368dp width as required. -->
        android:layout_width="368dp"
          
        <!-- unique ID of WebView -->
        android:id="@+id/web"
          
        <!-- covers 495dp height as required. -->
        android:layout_height="495dp"
          
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>

chevron_right


In AndroidManifest.xml, one needs to include below permission, in order to access internet:

"uses-permission android:name="android.permission.INTERNET"



My Personal Notes arrow_drop_up

Geeksforgeeks Intern

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.