Skip to content Skip to sidebar Skip to footer

Problem With Extra Space At The Bottom Of Android Webview

I have an Webview in my application where i display the html content using loadData() method. The Problem is HTML content is displayed along with some extra space at the bottom, I

Solution 1:

At last I found the answer for my own question, I need to append the meta tag along with my HTML content before adding it to the Webview. Please find the Code below

String s="<head><meta name='viewport' content='target-densityDpi=device-dpi'/></head>";            
 webview.loadDataWithBaseURL(null,s+htmlContent,"text/html" , "utf-8",null);      

The Viewport property inside the meta tag done the trick, please refer the following link for further details.

Using Viewport in Android Webview

Regards, Rajapandian

Solution 2:

This blog post solved my problem. I think it'll help. http://capdroid.wordpress.com/2014/08/07/resizing-webview-to-match-the-content-size/

privatevoidsetupWebView() {
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(newWebViewClient() {
        @OverridepublicvoidonPageFinished(WebView view, String url) {
            webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
            super.onPageFinished(view, url);
        }
    });
    webView.addJavascriptInterface(this, "MyApp");
}

@JavascriptInterfacepublicvoidresize(final float height) {
    MyActivity.this.runOnUiThread(newRunnable() {
        @Overridepublicvoidrun() {
            webView.setLayoutParams(newLinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
        }
    });
}

Solution 3:

Better use width property also .Because some devices consider that .

eg:

String s="<head><metaname=viewportcontent=target-densitydpi=medium-dpi,width=device-width/></head>";            

Solution 4:

For who set the viewport according to the accepted answer but still having problem, try removing padding and margin from HTML and BODY in pages inside the WebView. If you want to keep body padding, add box-sizing:border-box to body tag:

html{
    margin:0;padding:0;
}
body {
    margin:0;
    padding:20px;
    box-sizing:border-box;
}

Post a Comment for "Problem With Extra Space At The Bottom Of Android Webview"