Search code examples
androidsvgwebviewandroid-webviewandroid-websettings

Showing an svg in Android using a webview won't zoom out after zooming in


I am showing an svg in an android webview. With this, I am having a couple of problems.

I am getting reports of people not being able to zoom out, after they zoomed in. I don't want to put out another release until I know I have fixed the problem, but MY test device doesn't have this problem. Anytime I zoom in, I can zoom right back out. I'm testing on Android 4.4 though, and I think that introduced chrome as the default webview. The devices giving me errors are running at least api level 15, because thats what I have set in my manifest.

This my code with the problems I listed above.

private WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cartoon);


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

    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setDisplayZoomControls(false);
    myWebView.getSettings().setLoadWithOverviewMode(true);
    myWebView.setVerticalScrollBarEnabled(false);
        myWebView.setInitialScale(50);

    myWebView.setHorizontalScrollBarEnabled(false);

    myWebView.loadUrl("file:///android_res/raw/cartoon.svg");

So my question is does anyone know why my users aren't allow to zoom out after they zoom in?


Solution

  • Try this:

    myWebView = (WebView) findViewById(R.id.webView);
    myWebView.setInitialScale(1);
    WebSettings settings = myWebView.getSettings();
    settings.setSupportZoom(true);
    settings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
    settings.setUseWideViewPort(true);
    myWebView.getSettings().setBuiltInZoomControls(true);
    myWebView.getSettings().setDisplayZoomControls(false);
    myWebView.setVerticalScrollBarEnabled(false);
    
    myWebView.setHorizontalScrollBarEnabled(false);