Enable Disable JavaScript on WebView in android

On Off JavaScript functionality on webview to block pop-up ads on android device web browser.

Android webview gives us the facility to enable disable JavaScript functionality on webview so developer can perform both actions on webview. There is a function available on WebView named as setJavaScriptEnabled( pass true or false here ). By using this function if you will pass true then it will enable JavaScript on webview and if you pass flase in it then it will block the JavaScript. So here is the complete step by step tutorial for Enable Disable JavaScript on WebView in android.

android-project-download-code-button

Note: Add internet permission on AndroidManifest.xml file.

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

How to Enable Disable JavaScript on WebView in android.

Code for MainActivity.java file.

package com.android_example.com.webviewenablejavascript;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;


public class MainActivity extends Activity 
{
 WebView WebSiteView;
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 WebSiteView = (WebView)findViewById(R.id.webView1);
 WebSettings webSetting = WebSiteView.getSettings();
 
 // Set true for enable JavaScript feature or Set False to Disable JavaScript.
 webSetting.setJavaScriptEnabled(true);
 
 WebSiteView.setWebViewClient(new WebViewClient());
 WebSiteView.loadUrl("https://indiainfotip.com");
 }

 private class WebViewClient extends android.webkit.WebViewClient
 {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) 
 {
 return super.shouldOverrideUrlLoading(view, url);
 }
 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_example.com.webviewenablejavascript.MainActivity" >

 <WebView
 android:id="@+id/webView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

</RelativeLayout>

Code for AndroidManifest.xml. file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android_example.com.webviewenablejavascript"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET" />
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshot:

Enable Disable JavaScript on WebView in android

Click Here to Download Enable Disable JavaScript on WebView in android project.