How To Embed Android WebView to Load URLs Inside Android Apps

You’ve used Facebook, right?

Almost everybody has.

But the question is: have you ever clicked on any link shared by someone?

What happens when you click it?

The URL gets loaded within the Android app.

But why developers at Facebook implemented this feature?

There must be a reason, right?

Of course, there is. And the primary goal of Android Webview is to keep the users within the app.

And in today’s Android webview tutorial, we’ll be understanding how to embed Android webview in your native Android App.

Let’s Get Started

👉 Create a new project under file menu, modify the project details, and choose location for the demo project.

Create a new project

👉 In the next tab, choose Mini Support SDK for your project.

choose Mini Support SDK

👉 Lastly, select Add No Activity and click on finish button.

select Add No Activity

Start Code Integration

👉 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:id="@+id/buttonUrl"

android:layout_gravity="center"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="www.google.com" />

</LinearLayout>

👉 Web_view.xml

<?xml version="1.0" encoding="utf-8"?>

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/webView"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

Want To Create An Android Application?

Validate your app idea and get a free quote.

👉 MainActivity.java

package com.webviewdemo;

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private Button button;

public void onCreate(Bundle savedInstanceState) {

final Context context = this;

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.buttonUrl);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

Intent intent = new Intent(context, WebViewActivity.class);

startActivity(intent);

}

});

}

}

👉 WebViewActivity.java

package com.webviewdemo;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.web_view);

webView = (WebView) findViewById(R.id.webView);

webView.getSettings().setJavaScriptEnabled(true);

webView.loadUrl("https://www.google.com");

}

}

👉 AndroidManifest.xml

//Android Webview requires INTERNET permission.

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

Now once the user has given Internet permission, run the demo.

 Internet permission

Embed WebView in Android

And Done!

As simple as that!

However, if you’re building an Android app from scratch, and need technical help, talk with experts or hire android app developers to proceed with a professional approach under the right hands of experts.

Grab a free copy of Android webview example from Github.

Author Bio
Amit Patolia
Amit Patolia
Designation: Android Team Lead

Amit Patolia is an Android Team Lead at Space-O Technologies. He has over 9+ years of experience in Android app development. He has guided to develop over 100 Android apps with unique features and functionalities. He also has expertise in Kotlin-based apps.