android-webview-tutorial

Android Webview Tutorial With Example

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

With this android webview example, You can also create your own Webview app which can load any url just like a web browser. 

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 android webview app and change the Url to your own, if you have any question then comment below.