Introduction:

Want to open a website inside your Android app without launching Chrome? The WebView component is the way to go. In this tutorial, we’ll walk through how to integrate WebView into your Android app using Java in Android Studio.

Steps + Code:

1. Add Internet Permission in AndroidManifest.xml:

xmlCopyEdit<uses-permission android:name="android.permission.INTERNET" />

2. Create a Layout (activity_main.xml):

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

3. Set up the WebView in MainActivity.java:

import android.os.Bundle;
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);
webView.setWebViewClient(new WebViewClient()); // Stay in app
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.example.com");
}
}

And that’s it! You now have a simple in-app browser.

Leave a Reply

Your email address will not be published. Required fields are marked *