free web tracker
Course Content
Java Programming Basics for Android
Learn the basics of Java programming for Android app development using Android Studio. This guide covers key concepts like variables, loops, and classes to help you start building your first Android apps with confidence. Perfect for beginners!
0/10
User Interaction and App Logic
Learn how to make your app respond to users! This section covers handling clicks, getting input, showing messages, switching screens, and saving simple data. A perfect start to build interactive Android apps with real logic.
0/10
Advanced Layouts and Components
Learn to build modern Android UIs using advanced layouts like RecyclerView, CardView, TabLayout, and more. This section helps beginners create beautiful, interactive, and user-friendly app interfaces step by step.
0/10
Media and Resources
Learn how to manage media and resources in Android Studio. This section covers adding audio, images, video, using drawables, custom fonts, and handling runtime permissions—essential for building rich, engaging Android applications.
0/4
Mastering Java Android Development – Beginner

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.