16

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);
        }
    });
2
  • 1
    plus one for the question.. Jun 14, 2016 at 8:45
  • Thanks for the question and the solution! Your snippet can actually enable the webview zoom by fingers, however it adds an additional zoom in/out button besides the webview native ones. Do you know any way to disable them?
    – thahgr
    Mar 9, 2021 at 11:38

7 Answers 7

17

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.

2
  • 1
    Thanks for the reply! I ended up having to inject some Javascript into the page just like you said. See my update above to view the exact code I put in it.
    – Mr. Spock
    Dec 2, 2014 at 21:34
  • Your answer helped me, plus 1 Jun 14, 2016 at 8:45
9

Add these two lines to enable pinch to zoom in webview

webview.getSettings().setSupportZoom(true);
webview.getSettings().setBuiltInZoomControls(true);
0
5

Have you tried this suggestion?

webSettings.setSupportZoom(true);

See Disable pinch zoom in WebView?

Note the link is in regards for disabling zoom. But perhaps your zoom is being disabled by default as some users suggest.

1
  • Thanks for the reply! Unfortunately, this does not work. Like I said above, I'm a bit perplexed why I can load a different page in the exact WebView in the exact Activity and it zooms just fine. At first glance, this would seem to implicate the web page itself as the source of the problem.
    – Mr. Spock
    Dec 1, 2014 at 20:47
4

On my issue, I succeed with above JavaScript injection due to viewport restrictions. Somehow onPageFinished didn't worked in my case but onLoadResource did the trick.

@Override
public void onLoadResource(WebView view, String url) {
    view.loadUrl("javascript:document.getElementsByName(\"viewport\")[0].setAttribute(\"content\", \"width=device-width, initial-scale=1.0, maximum-scale=5.0, user-scalable=yes\");");
    super.onLoadResource(view, url);
}
1
  • 1
    I have been finding the solution to this problem for a very long time! I had given up but just now came across this answer and it worked like a charm!
    – FutureJJ
    May 27, 2022 at 19:08
3
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setDisplayZoomControls(false);

worked flawlessly for me.

1

For Zoom:

webSettings.getSettings().setBuiltInZoomControls(true);

For Disable:

webSettings.getSettings().setBuiltInZoomControls(false);
0

To allow zoom at Android WebView the only settings needed are:

  1. webView.getSettings().setBuiltInZoomControls(true);
  2. webView.getSettings().setDisplayZoomControls(false);
  3. Make sure that HTML header viewport has proper values;
  4. If you are changing the settings after load, make sure call from UIThread;

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.