Nasser AlSalmi
Nasser AlSalmi

Reputation: 21

Fit Webview content to screen

I've a webview that loads a website from local assets folder:

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

    //webView.setInitialScale(1);
    //webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
    //webView.getSettings().setLoadWithOverviewMode(true);
    //webView.getSettings().setUseWideViewPort(true);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);

    webView.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            webView.setVisibility(View.VISIBLE);
        }
    });

    if ((Locale.getDefault().getLanguage()).contains("ar"))
        webView.loadUrl("file:///android_asset/Index-ar.html");
    else
        webView.loadUrl("file:///android_asset/Index-en.html");

By default, the webpage is zoomed in and the user have to scroll and all what I want is to fit the page width to the webview width (which is full screen).

The comments shows you what I've tried:

setInitialScale(1) hides all the content of the page!

setDefaultZoom(WebSettings.ZoomDensity.FAR) changes nothing.

setLoadWithOverviewMode(true) and setUseWideViewPort(true) zooms out to much.

Any suggestions ?

Upvotes: 2

Views: 12281

Answers (3)

pratik
pratik

Reputation: 211

Its little late, but it can help some who needs it.

Add following in head tag of the webpage :

<meta name="viewport" content='width=device-width, initial-scale=1.0,text/html,charset=utf-8' >

Then add this in activity :

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

webView.setInitialScale(1);

webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);

If you want to support zoom buttons you can add this –

webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);

Upvotes: 9

Haresh Chaudhary
Haresh Chaudhary

Reputation: 4400

Could you please try this :

web.getSettings().setBuiltInZoomControls(true);
web.getSettings().setSupportZoom(true); 
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);

Upvotes: 4

aandrei
aandrei

Reputation: 165

Have you tried to do that by xml? Set layout width and height to fill_parent.

<WebView  
      android:id="@+id/webView"
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/>

Upvotes: -1

Related Questions