Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with extra space at the bottom of android Webview

Tags:

android

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 could not understand why the white space is coming. This problem is occurring only in Motorola Milestone device(Android 2.1). Please somebody help to sort out this problem.

enter image description here

Thanks in Advance, Rajapandian

like image 896
Rajapandian Avatar asked Aug 24 '11 08:08

Rajapandian


People also ask

Is Android WebView slow?

Using WebViews in your native application is very common these days but when it comes to performance, rendering of a WebView is quite slow. As most of the heavy lifting done by WebView is loading images.

How do I close WebView on Android?

Add a close button and on its click set: webview. setVisibility(View. INVISIBLE); webview.


Video Answer


3 Answers

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

like image 175
Rajapandian Avatar answered Oct 05 '22 16:10

Rajapandian


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/

   private void setupWebView() {
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)");
            super.onPageFinished(view, url);
        }
    });
    webView.addJavascriptInterface(this, "MyApp");
}

@JavascriptInterface
public void resize(final float height) {
    MyActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            webView.setLayoutParams(new LinearLayout.LayoutParams(getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density)));
        }
    });
}
like image 23
Can Uludağ Avatar answered Oct 05 '22 15:10

Can Uludağ


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

eg:

String s="<head><meta name=viewport content=target-densitydpi=medium-dpi, width=device-width/></head>";            
like image 38
KP_ Avatar answered Oct 05 '22 16:10

KP_