WebView in Android

If you’re new to Android development, you may be wondering what a webview is and how to use it in your app. So, in this post, I will tell you about what a WebView is and how you can use it in your android project. So, without further ado, let’s get started!

What is Android WebView?

A webview is an android view that allows you to display a web page within your app.

In simple words, if you want a simple way to display a web page within your app then you can use Android WebView.

How to use Android WebView?

Don’t forget to include Internet uses-permission in your manifest file, otherwisw you would end up getting “webpage not available” error.

Include the below line in your manifest file just before the <application> tag.

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

Now, let’s add webview in xml layout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Now, I will load google.com when our app starts. Let’s do this in our MainActivity.

package com.example.webview_example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webView = findViewById(R.id.webview);
        webView.loadUrl("https://www.google.com/");
    }
}

Output :

That’s how you can display a webpage within your app.

Goodbye.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: