WebView
is a powerful component in Android that allows you to display web pages as a part of your application. This guide walks you through how to use WebView in Android Studio using Java to display websites or custom HTML content directly inside your app.
1. Modify the Layout File
Open res/layout/activity_main.xml
and add a WebView
:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
2. Add Internet Permission
Open AndroidManifest.xml
and add this permission above the <application>
tag:

<uses-permission android:name="android.permission.INTERNET"/>
3. Configure WebView in Java
Open MainActivity.java
and configure the WebView:

package com.example.webviewdemo; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = findViewById(R.id.webView); // Enable JavaScript WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); // Load a website webView.setWebViewClient(new WebViewClient()); webView.loadUrl("https://www.example.com"); } // Optional: Handle back button @Override public void onBackPressed() { if (webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); } } }
Tips
- Always test URLs in different network conditions.
- Consider adding error handling with
WebViewClient.onReceivedError
. - To load custom HTML:
webView.loadData("<h1>Hello WebView</h1>", "text/html", "UTF-8");
Conclusion

Using WebView
in Android allows you to embed web content into your apps with ease. This can be useful for tutorials, support pages, or hybrid apps. Always be cautious of security when loading external content.