Android Webview Tutorial With Example

Have you seen the Android app displaying Web content using Webview. This Android Webview Tutorial is about Webview. Webview is an extension of Android’s View that allows users to display a website as part of the activity.

You can also use Webview in your application. And if you want to use it in your app so in this post i will guide you about implementing Webview in an Android application.

Create a new project

  • As for every new application we need to create a new Android Studio project. For this i have created a new project
  • After creating new project we need to add internet permission todo so add the below line in your AndroidManifest.xml just above your Application tag
<uses-permission android:name="android.permission.INTERNET"/>

We need the internet to display web content in WebView, so for accessing the internet we added the above permission in our AndroidManifest.xml.

Implementation of Webview

 Android Webview layout

   Open res/layout/activity_main.xml and write the following code

<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"
   tools:context=".WebViewActivity">

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

</LinearLayout>

The Android Webview component is added into the layout file in which we want to display Webview.

Android Webview code

Open MainActivity.java and initialize Webview using its id defined in activity_main.xml as shown in the snippet below.

WebView myWebView = findViewById(R.id.my_webView);

Android Webview loadUrl

  Once the Webview reference is obtained we can load Urls by using the Webview loadUrl() method

myWebView.loadUrl(“https://www.programmingtunes.com”);

Before loading Url there is one thing we need to take a look at, And that is Supporting JavaScript in webview.

By default the JavaScript is turned off in webview so the pages containing javaScript won’t load perfectly. So the following snippet will be called to enable javaScript.

myWebView.getSettings().setJavaScriptEnabled(true);

Complete MainActivity.java containing all the features discussed above.

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 myWebView = findViewById(R.id.my_webView);
       myWebView.getSettings().setJavaScriptEnabled(true);

       myWebView.loadUrl("https://www.programmingtunes.com/");//url loading
   }
}

Now run your app and change the Url with your own, if you have any question then comment below.