137

The default Browser app for Android shows zoom controls when you're scrolling and also allows for pinch zooming. How can I enable this feature for my own Webview?

I've tried:

webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);

but neither of the features get enabled as a result. Btw I've set a WebChromeClient and a WebViewClient for the Webview if that makes a difference.

Thanks!

Vivek Kalkur
  • 2,176
  • 2
  • 19
  • 40
Robert Bana
  • 1,995
  • 3
  • 14
  • 23

6 Answers6

304

Strange. Inside OnCreate method, I'm using

webView.getSettings().setBuiltInZoomControls(true);

And it's working fine here. Anything particular in your webview ?

zov
  • 3,884
  • 1
  • 13
  • 19
  • 46
    While this will allow pinch-to-zoom, it will also display a zoom overlay control (Galaxy S3). In order to disable the on-screen zoom tool, but retain the pinch-to-zoom functionality, you need to call webView.setDisplayZoomControls(false) as well. – Lev Nov 06 '14 at 09:38
  • 50
    ^ `webView.getSettings().setDisplayZoomControls(false);` – wiseindy Apr 18 '15 at 18:26
  • All of these don't work for me. Try this website for example: "https://www.nbc26.com/robocalls/battling-robocalls-breaking-down-relentless-and-annoying-calls" . It doesn't let me zoom. Does it really work for you? please show entire code for it. – android developer May 22 '19 at 14:35
  • If anyone is looking at this answer and wondering where the webview object comes from, you can get it via `WebView webView = (WebView) appView.getEngine().getView();` as shown in [this comment.](https://stackoverflow.com/a/36464728/7407567) – Andrew Dant May 24 '19 at 17:14
  • If you use Xamarin.Android web view renderer then it's code as below Control.Settings.BuiltInZoomControls = true; Control.Settings.SetSupportZoom(true); Control.Settings.DisplayZoomControls = false; – Adil Saiyad Mar 16 '20 at 06:52
103

Use these:

webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setDisplayZoomControls(false);
Dario Brux
  • 1,649
  • 1
  • 15
  • 14
36

Check if you don't have a ScrollView wrapping your Webview.

In my case that was the problem. It seems ScrollView gets in the way of the pinch gesture.

To fix it, just take your Webview outside the ScrollView.

7

Inside OnCreate, add:

 webview.getSettings().setSupportZoom(true);
 webview.getSettings().setBuiltInZoomControls(true);
 webview.getSettings().setDisplayZoomControls(false);

Inside the html document, add:

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2, user-scalable=yes">
</head>
</html>

Inside javascript, omit:

//event.preventDefault ? event.preventDefault() : (event.returnValue = false);
Jarir
  • 227
  • 2
  • 8
  • The default boolean for setSupportZoom is `true`, therefor no need for `webview.getSettings().setSupportZoom(true);` by default. – زياد Apr 24 '20 at 19:09
  • good answer! you mentioned the HTML related issues, this is the key solving my problem! thank you ! – Siwei May 31 '20 at 10:25
5

To enable zoom controls in a WebView, add the following line:

webView.getSettings().setBuiltInZoomControls(true);

With this line of code, you get the zoom enabled in your WebView, if you want to remove the zoom in and zoom out buttons provided, add the following line of code:

webView.getSettings().setDisplayZoomControls(false);
Anubhav
  • 1,140
  • 14
  • 10
4

Try this code, I get working fine.

 webSettings.setSupportZoom(true);
 webSettings.setBuiltInZoomControls(true);
 webSettings.setDisplayZoomControls(false);
Muhammed Haris
  • 278
  • 3
  • 14