Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable zoom when clicking on form fields within a WebView?

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.

like image 625
tplaner Avatar asked Jan 28 '11 14:01

tplaner


3 Answers

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);
    }
}
like image 54
Moak Avatar answered Oct 07 '22 19:10

Moak


I solved this on HTC phones by adding a WebViewClient with an empty listener for onScaleChanged. My app is PhoneGap, so this is what it looks like, but adding the listener should look the same in a non-PhoneGap app:

public class Main extends DroidGap {

private class NoScaleWebViewClient extends GapViewClient {

    public NoScaleWebViewClient(DroidGap ctx) {
        super(ctx);
    }

    public void onScaleChanged(WebView view, float oldScale, float newScale) {
        Log.d("NoScaleWebViewClient", "Scale changed: " + String.valueOf(oldScale) + " => " + String.valueOf(newScale));
    }
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.init();
    setWebViewClient(appView, new NoScaleWebViewClient(this));
    // disables the actual onscreen controls from showing up
    appView.getSettings().setBuiltInZoomControls(false);
    // disables the ability to zoom
    appView.getSettings().setSupportZoom(false);
    appView.getSettings().setDefaultZoom(ZoomDensity.FAR);
    appView.setInitialScale(100);
}

}

Strangely, the onScaleChange listener never gets called -- by listening for the zoom, it blocks the zoom from happening. I've found that I need all the other calls (setSupportZoom, setDefaultZoom, setInitialScale) in order for this to work, and removing any of them reverts to the old, buggy behavior.

like image 23
incidentist Avatar answered Oct 07 '22 21:10

incidentist


I had the same trouble. I needed to find a way to scale content of webview to exact value, everything worked fine until user starts to input text. There are methods that work on relatively new devices android 4.0+ but fails on old ones. The only way that works everywhere is setting the zoom value not in Java but in viewport like this

<meta name="viewport" content="initial-scale=.80; maximum-scale=.80; minimum-scale=.80;" />

It works on every device I tested.

like image 24
Alexander Ponomarev Avatar answered Oct 07 '22 21:10

Alexander Ponomarev