6

I am developing the application using PhoneGap. I cannot enable built in zoom in/out in the webview.

I used Following code in onCreate Function

WebView web = (WebView) findViewById(R.id.webview);
web.getSettings().setBuiltInZoomControls(true);

But it did not work.

And The Activity class is

activity_main.xml

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/> 

3 Answers 3

19

Check if you don't have a ScrollView wrapping your Webview.

It seems ScrollView gets in the way of the pinch gesture.

To fix it, just take your Webview outside the ScrollView nd then use the same line:

webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
6
  • my activity_main.xml is having only webView. The above code is the entire activity_main.xml
    – Manu
    Nov 19, 2013 at 10:13
  • Then this line should wrk webView.getSettings().setBuiltInZoomControls(true); Nov 19, 2013 at 10:15
  • am getting web page with scrolling in horizontal and vertical. using above line still not enable zoom
    – Manu
    Nov 19, 2013 at 10:49
  • the zooming is now working. When i changed the webview to CordovaWebView the zooming is working.
    – Manu
    Nov 19, 2013 at 11:25
  • 4
    Worked for me. Also a tip for those who want the pinch zoom but without the zoom icons displayed: webSettings.setDisplayZoomControls(false); May 29, 2015 at 10:48
6

For Cordova 5

This has changed slightly for Cordova 5.1 (I think it changed with 5.0 actually).
To enable Android zooming for Cordova 5, add these lines :

import android.webkit.WebView;
import android.webkit.WebSettings; 
import android.webkit.WebSettings.ZoomDensity;

and these

        WebView webView = (WebView) appView.getEngine().getView();
        WebSettings settings = webView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);

A full sample of your src/com/YOURPACKAGE.java file:

package com.YOURPACKAGE;

import android.os.Bundle;
import org.apache.cordova.*;

import android.webkit.WebView;
import android.webkit.WebSettings; 
import android.webkit.WebSettings.ZoomDensity;


public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);

        WebView webView = (WebView) appView.getEngine().getView();
        WebSettings settings = webView.getSettings();
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        //settings.setDefaultZoom(ZoomDensity.FAR);
    }
}
0

Use ==>>

web.getSettings().setBuiltInZoomControls(true);

before the line ==>>

web.loadDataWithBaseURL("Your Required params");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.