Enabling JavaScript And Zoom Control Of WebView

This example shows how to enable JavaScript and built-in zoom control of WebView. To do this we can call setJavaScriptEnabled(true) and setBuiltInZoomControls(true) functions of WebSettings class.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it JavaScriptAndZoomControl.

2.) Write following into main.xml:

 

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

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

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="Hello"

/>

<WebView

android:id="@+id/webview"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

</LinearLayout>

 

 

3.) Add following permission to manifest file:

 

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

 

4.) Run for output.

Steps:

1.) Create a project named JavaScriptAndZoomControl and set the information as stated in the image.

Build Target: Android 4.4
Application Name: JavaScriptAndZoomControl
Package Name: com.example.JavaScriptAndZoomControl
Activity Name: JavaScriptAndZoomControlActivity

zoom1

2.) Open JavaScriptAndZoomControlActivity.java file and write following code there:

 

package com.example.javascriptandzoomcontrol;

import android.app.Activity;

import android.os.Bundle;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class JavaScriptAndZoomControlActivity extends Activity {
WebView webView;

final String DEFAULT_URL = "https://www.edumobile.org/android/";

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

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

webView.getSettings().setJavaScriptEnabled(true);

webView.getSettings().setBuiltInZoomControls(true);

webView.setWebViewClient(new MyWebViewClient());

webView.loadUrl(DEFAULT_URL);

}

public class MyWebViewClient extends WebViewClient {

@Override

public boolean shouldOverrideUrlLoading(WebView view, String url) {

// TODO Auto-generated method stub

view.loadUrl(url);

return true;

}

}

}

 

 

3.) Compile and build the project.

Output

zoom2