MainActivity.java

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());
         
    }
}

activity_main.xml

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

<?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>

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

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

If there is any problem please don't hesitate to message me.
How to use WebView in Android Javahttps://blog.herbydesign.com/wp-content/uploads/2021/08/google-chrome-android-app-os-2-720x480.jpghttps://blog.herbydesign.com/wp-content/uploads/2021/08/google-chrome-android-app-os-2-720x480-300x300.jpg Hernan Malubay Android StudioWebsites
MainActivity.java 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...
<strong>MainActivity.java</strong> <div class="noIdeBtnDiv"> <div class="code-block"> <div class="code-gutter"></div> <div class="code-container"> <div id="highlighter_937092" class="syntaxhighlighter nogutter"> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="code"> <div class="container"> <div class="line number1 index0 alt2"><code class="keyword">package</code> <code class="plain">com.example.hp.webview;</code></div> <div class="line number2 index1 alt1"><code class="undefined spaces"> </code></div> <div class="line number3 index2 alt2"><code class="keyword">import</code> <code class="plain">android.support.v7.app.AppCompatActivity;</code></div> <div class="line number4 index3 alt1"><code class="keyword">import</code> <code class="plain">android.os.Bundle;</code></div> <div class="line number5 index4 alt2"><code class="keyword">import</code> <code class="plain">android.webkit.WebView;</code></div> <div class="line number6 index5 alt1"><code class="keyword">import</code> <code class="plain">android.webkit.WebViewClient;</code></div> <div class="line number7 index6 alt2"><code class="undefined spaces"> </code></div> <div class="line number8 index7 alt1"><code class="keyword">public</code> <code class="keyword">class</code> <code class="plain">MainActivity </code><code class="keyword">extends</code> <code class="plain">AppCompatActivity {</code></div> <div class="line number9 index8 alt2"><code class="undefined spaces"> </code></div> <div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="color1">@Override</code></div> <div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="keyword">protected</code> <code class="keyword">void</code> <code class="plain">onCreate(Bundle savedInstanceState) {</code></div> <div class="line number12 index11 alt1"><code class="undefined spaces">        </code><code class="keyword">super</code><code class="plain">.onCreate(savedInstanceState);</code></div> <div class="line number13 index12 alt2"><code class="undefined spaces">         </code></div> <div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="comments">// Binding MainActivity.java with </code></div> <div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="comments">// activity_main.xml file</code></div> <div class="line number16 index15 alt1"><code class="undefined spaces">        </code><code class="plain">setContentView(R.layout.activity_main);</code></div> <div class="line number17 index16 alt2"><code class="undefined spaces">         </code></div> <div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="comments">// Find the WebView by its unique ID</code></div> <div class="line number19 index18 alt2"><code class="undefined spaces">        </code><code class="plain">WebView w = (WebView) findViewById(R.id.web);</code></div> <div class="line number20 index19 alt1"><code class="undefined spaces">         </code></div> <div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="comments">// loading <a href="http://www.google.com/">http://www.google.com</a> url in the the WebView.</code></div> <div class="line number22 index21 alt1"><code class="undefined spaces">        </code><code class="plain">w.loadUrl(</code><code class="string">"<a href="http://www.google.com/">http://www.google.com</a>"</code><code class="plain">);</code></div> <div class="line number23 index22 alt2"><code class="undefined spaces">         </code></div> <div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="comments">// this will enable the javascipt.</code></div> <div class="line number25 index24 alt2"><code class="undefined spaces">        </code><code class="plain">w.getSettings().setJavaScriptEnabled(</code><code class="keyword">true</code><code class="plain">);</code></div> <div class="line number26 index25 alt1"><code class="undefined spaces">         </code></div> <div class="line number27 index26 alt2"><code class="undefined spaces">        </code><code class="comments">// WebViewClient allows you to handle </code></div> <div class="line number28 index27 alt1"><code class="undefined spaces">        </code><code class="comments">// onPageFinished and override Url loading.</code></div> <div class="line number29 index28 alt2"><code class="undefined spaces">        </code><code class="plain">w.setWebViewClient(</code><code class="keyword">new</code> <code class="plain">WebViewClient());</code></div> <div class="line number30 index29 alt1"><code class="undefined spaces">         </code></div> <div class="line number31 index30 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div> <div class="line number32 index31 alt1"><code class="plain">}</code></div> </div></td> </tr> </tbody> </table> </div> </div> </div> </div> <strong>activity_main.xml</strong> In the xml file only use of WebView is made inside RelativeLayout. <div class="noIdeBtnDiv"> <div class="code-block"> <div class="code-container"> <div id="highlighter_983040" class="syntaxhighlighter nogutter"> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="code"> <div class="container"> <div class="line number1 index0 alt2"><code class="plain"><?</code><code class="keyword">xml</code> <code class="color1">version</code><code class="plain">=</code><code class="string">"1.0"</code> <code class="color1">encoding</code><code class="plain">=</code><code class="string">"utf-8"</code><code class="plain">?></code></div> <div class="line number2 index1 alt1"><code class="plain"><</code><code class="keyword">RelativeLayout</code></div> <div class="line number3 index2 alt2"><code class="undefined spaces">    </code><code class="color1">xmlns:android</code><code class="plain">=</code><code class="string">"<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"</code></div> <div class="line number4 index3 alt1"><code class="undefined spaces">    </code><code class="color1">xmlns:tools</code><code class="plain">=</code><code class="string">"<a href="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"</code></div> <div class="line number5 index4 alt2"><code class="undefined spaces">    </code><code class="color1">xmlns:app</code><code class="plain">=</code><code class="string">"<a href="http://schemas.android.com/apk/res-auto">http://schemas.android.com/apk/res-auto</a>"</code></div> <div class="line number6 index5 alt1"><code class="undefined spaces">    </code><code class="color1">android:layout_width</code><code class="plain">=</code><code class="string">"match_parent"</code></div> <div class="line number7 index6 alt2"><code class="undefined spaces">    </code><code class="color1">android:layout_height</code><code class="plain">=</code><code class="string">"match_parent"</code></div> <div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="color1">tools:context</code><code class="plain">=</code><code class="string">"com.example.hp.webview.MainActivity"</code><code class="plain">></code></div> <div class="line number9 index8 alt2"><code class="undefined spaces"> </code></div> <div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="plain"><</code><code class="keyword">WebView</code></div> <div class="line number11 index10 alt2"><code class="undefined spaces">        </code><code class="comments"><!-- covers 368dp width as required. --></code></div> <div class="line number12 index11 alt1"><code class="undefined spaces">        </code><code class="plain">android:layout_width="368dp"</code></div> <div class="line number13 index12 alt2"><code class="undefined spaces">         </code></div> <div class="line number14 index13 alt1"><code class="undefined spaces">        </code><code class="comments"><!-- unique ID of WebView --></code></div> <div class="line number15 index14 alt2"><code class="undefined spaces">        </code><code class="plain">android:id="@+id/web"</code></div> <div class="line number16 index15 alt1"><code class="undefined spaces">         </code></div> <div class="line number17 index16 alt2"><code class="undefined spaces">        </code><code class="comments"><!-- covers 495dp height as required. --></code></div> <div class="line number18 index17 alt1"><code class="undefined spaces">        </code><code class="plain">android:layout_height="495dp"</code></div> <div class="line number19 index18 alt2"><code class="undefined spaces">         </code></div> <div class="line number20 index19 alt1"><code class="undefined spaces">        </code><code class="plain">tools:layout_editor_absoluteX="8dp"</code></div> <div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="plain">tools:layout_editor_absoluteY="8dp" /></code></div> <div class="line number22 index21 alt1"><code class="plain"></</code><code class="keyword">RelativeLayout</code><code class="plain">></code></div> </div></td> </tr> </tbody> </table> </div> </div> </div> </div> In <strong>AndroidManifest.xml</strong>, one needs to include below permission, in order to access internet: <pre>"uses-permission android:name="android.permission.INTERNET" If there is any problem please don't hesitate to message me.</pre>