25

I'm using a webview in my android app, at the moment when the app is started the website is zoomed in quite a lot, i want it to be zoomed out to fit the width of the screen. I currently have this in my activity:

super.onCreate(savedInstanceState);
    setContentView(R.layout.shop);
    WebView webview;
    webview = (WebView) findViewById(R.id.webview);
    webview.setWebViewClient(new WebViewClient());
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("http://www.example.com");
Carla Dessi
  • 9,086
  • 9
  • 39
  • 53

7 Answers7

50
webview.getSettings().setLoadWithOverviewMode(true);    

This will cause the webview to be zoomed out initially.

webview.getSettings().setUseWideViewPort(true);

The Webview will have a normal viewport (like desktop browser), when false the webview will have a viewport constrained to it's own dimensions.

EDIT: With the introduction of "Chrome web view" in Android KitKat, this code might not work.

Shakti
  • 1,581
  • 2
  • 20
  • 33
  • 2
    I used this with `wv.getSettings().setBuiltInZoomControls(true);` per [this answer](http://stackoverflow.com/a/7903262/536986) to default my view to zoomed out and to be able to pinch-to-zoom. – Cameron Sep 26 '13 at 22:43
  • Refer this link https://code.google.com/p/android/issues/detail?id=92841 in .xml file you should set android:layout_width="match_parent" and android:layout_height="match_parent" for webview. If you set android:layout_height="wrap_content", webview doesn't auto zoom out in android 4.4 – Anh Duy Feb 07 '17 at 09:20
19

This zooms out so that the content (an SVG in my case) fits on the screen but does not make unnecessary space.

webView.getSettings().setUseWideViewPort(true);
webView.setInitialScale(1);
aleb
  • 2,490
  • 1
  • 28
  • 46
9

Try this:

    webView.setInitialScale(50);
    webPlanSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
    webPlanSettings.setUseWideViewPort(true);
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
7
    //This the the enabling of the zoom controls 
    webView.getSettings().setBuiltInZoomControls(true);

    //This will zoom out the WebView
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.setInitialScale(1);
Papa Yev
  • 618
  • 7
  • 10
3

For Kit Kat and later devices you need to set the viewport meta tag in the headers of the HTML page loaded by your WebView in order to prevent the default zoom in behavior.

<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
Matt
  • 688
  • 5
  • 12
2

use the webSettings class to set the zoom level...

webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
Zohaib Brohi
  • 576
  • 1
  • 7
  • 15
0

Set WebView settings like:

webView.settings.run {
    builtInZoomControls = true
    //hide +- zoom buttons
    displayZoomControls = false 
    //zooms out the content to fit on screen by width
    loadWithOverviewMode = true 
    //when page contains the viewport meta tag
    useWideViewPort = true 
}

But consider that zoom settings above, work only for WebView first run.
If you want to full zoom out every time you load a URL in WebView (I faced this case), use zoomBy like this

webView.loadUrl(url)
webView.zoomBy(0.02f)
  • By adding the code, i can able to scroll the view hence the zoom out is not applied – arun Sep 01 '23 at 12:03