[FIXED] Why does pinch-to-zoom not work in my Android WebView?

Issue

I have an activity with a layout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <include  layout="@layout/window_title" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" 
        android:layout_below="@+id/linear_layout"/>

</RelativeLayout>

Here is how I’m configuring it:

    // Enable JavaScript.
    WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Settings so page loads zoomed-out all the way.
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setBuiltInZoomControls(true);

Here’s the version setting from my manifest file:

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

I’m trying to load this page in the WebView:

https://new.livestream.com/lcboise

The page loads just fine, but I can’t pinch to zoom. I’ve tried different combinations of the WebView settings (above, including others not listed) but it just won’t zoom.

Observations:

1) Loading up a different page I’m using (https://lcboise.infellowship.com/UserLogin) in the EXACT same WebView allows me to zoom.

2) My main test device, where is DOES NOT work, is a HTC One running v4.4.3 of Android.

3) I can load, and zoom, the livestream page on an older test device I’m using that’s running v2.3.3 of Android.

Is it possible that something in the page itself is breaking the WebView running on the HTC One? If so, any guesses as to might be doing it?

Update [SOLUTION]:

Here is what I had to add to my WebView to get pinch-to-zoom to work:

    myWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            String javascript="javascript:document.getElementsByName('viewport')[0].setAttribute('content', 'initial-scale=1.0,maximum-scale=10.0');";
            view.loadUrl(javascript);
        }
    });

Solution

This is the page you linked has the following viewport meta tag:

<meta name="viewport" content="width=1024, maximum-scale=1.0, target-densitydpi=device-dpi">

The page that works has a different viewport meta tag. The maximum-scale bit is telling the WebView to not allow zooming in more than the specified amount.

The site should also be broken in any modern mobile browser. Setting maximum-scale to a low value like that is not very "mobile friendly" so it might just be a bug on the site. Have you tried contacting the owner, maybe they can fix it server-side?

There is not a whole lot you can do in the WebView that will not result in other sites rendering incorrectly. You could try injecting JavaScript to fix up the page by changing the meta tag as a last resort.

Answered By – marcin.kosiba

Answer Checked By – Terry (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published