Disable zoom when clicking on form fields within a WebView?

tplaner picture tplaner · Jan 28, 2011 · Viewed 19.7k times · Source

I've looked through dozens of pages if similar questions, none of them have any answers, so hopefully this one will be different.

I have a webview, and I do not want the zoom of the view to change from the initial zoom level I have it set to. The only thing which changes the zoom level currently is when a text box is focused.

I need to be able to do this through Java code, not using the viewport meta tag.

Just so I don't have the common responses, I have the following in my code to disable zooming, and the zoom controls:

mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.getSettings().setSupportZoom(false);

I'm thinking that a possible solution is to check to see when an onFocus or even an onClick event occurs within the WebView and then zoomOut, but I'm not even sure if that is possible?

Any suggestions would be appreciated.

Answer

Moak picture Moak · Oct 23, 2011

UPDATE This answer was written almost 6 years ago, with all the new android versions that came since then, this is most likely outdated.

This thing caused a major headache, but finally was solved thanks to setDefaultZoom(ZoomDensity.FAR);

One thing which is important is that onCreate and loadUrl get called before the WebSettings, otherwise it caused a force close situation. Here the ENTIRE code including imports (for the novice Java users)

package com.my.app;

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;

import com.phonegap.*;

public class MyDroidActivity extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        WebSettings settings = appView.getSettings();
        settings.setBuiltInZoomControls(false);
        settings.setSupportZoom(false);
        settings.setDefaultZoom(ZoomDensity.FAR);
    }
}