7

I am using a webview in my android app, however after it finishes loading - which I can detect via onPageFinished(WebView webview, String url) - the page continues to infinitely grow in height.

How can I prevent this from happening?

Here's what I do.

  1. I have a generic page template which contains a ScrollView. Width and height are set to match parent. Inside the ScrollView there are several common elements for all the activities I create. One of them is a LinearLayout where I insert all the user content.

    .....
    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:layout_margin="0dp"
    android:padding="0dp"
    android:scrollbars="none"
    >
    
    ......
    
    <LinearLayout 
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:orientation="vertical">
    
            //EVERY USER CONTENT GOES HERE    
    
    </LinearLayout>
    
    
    ......
    
    </ScrollView>
    

    .....

  2. In this particular case the content is a web page, that loads Google maps using the mobile web API. Here is the layout file

    <?xml version="1.0" encoding="utf-8"?>
    
      <WebView
           android:id="@+id/maps"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:layout_margin="0dp"
           android:padding="0dp"
      />
    
  3. Here's the code of my activity that initialises the webview

    .... WebView wv = (WebView)this.content.findViewById(R.id.maps);

    WebSettings ws = wv.getSettings();

    ws.setPluginState(PluginState.ON);
    ws.setSupportZoom(false);
    ws.setLightTouchEnabled(true);
    ws.setDomStorageEnabled(true);
    ws.setAppCacheMaxSize(1024 * 1024 * 8);
    ws.setAppCachePath(context.getCacheDir().getAbsolutePath());
    ws.setAppCacheEnabled(true);
    ws.setAllowFileAccess(true);
    ws.setCacheMode(WebSettings.LOAD_NORMAL);
    ws.setJavaScriptEnabled(true);
    
    wv.setInitialScale(0);
    wv.addJavascriptInterface(new Object(), "Android");
    wv.setKeepScreenOn(true);
    
  4. Finally, when I load the maps via wv.loadUrl("......");

after the page finishes loading I get the following logs and the webview grows in an infinite loop

02-18 14:16:06.195: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.394: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.480: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.527: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.582: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.632: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.683: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.734: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.785: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.875: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:06.957: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.058: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.332: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.398: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.449: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.496: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.554: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.605: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.652: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.707: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.742: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.792: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.839: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.902: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:07.949: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.015: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.066: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.113: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.164: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.214: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.250: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.300: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.335: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.386: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.437: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.484: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.531: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.566: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.613: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.667: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.714: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.750: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.847: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.902: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:08.949: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.003: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.035: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.085: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.121: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.167: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.218: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.253: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.304: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.367: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.417: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.476: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.523: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.585: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.632: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.664: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.710: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.765: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.812: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.863: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.894: VERBOSE/webview(10904): OnSizeChanged: Enter
02-18 14:16:09.957: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:09.992: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.042: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.097: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.148: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.179: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.226: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.277: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.328: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.378: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.410: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.460: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.511: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.546: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.597: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.648: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.679: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.730: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.781: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.835: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.886: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.925: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:10.976: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.011: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.066: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.125: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.183: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.214: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.265: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.320: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.371: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.402: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.453: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.507: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.558: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.589: VERBOSE/webview(10904): OnSizeChanged: Enter 
02-18 14:16:11.644: VERBOSE/webview(10904): OnSizeChanged: Enter 
.......... This goes on ...........

I have noticed that this happens when the page contains jQuery. But the same page loads fine in the stock browser.

Any idea why this happens, and how can this be rectified?

Any meaningful help is appreciated... Thanks.

Nar Gar
  • 2,591
  • 2
  • 25
  • 28

4 Answers4

6

This is a late answer but hopefully it helps someone. I was experiencing the same problem and the culprit was this underlying CSS:

body {
    width: 100%;    
    height: 100%;
}

This change fixes the issue:

body {
    width: 100%;    
    height: auto;
}
Nachi
  • 4,218
  • 2
  • 37
  • 58
3

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)));
        }
    });
}
Can Uludağ
  • 705
  • 8
  • 14
0

I faced the same issue and solved after remove scrollview, if webview inside scrollview or nestedscrollview u have to edit xml and remove scrolling

Mostafa Anter
  • 3,445
  • 24
  • 26
-4

Did you try android:layout_height="50dip"

Pass value for height attribute rather then using wrap_content

Hardik Trivedi
  • 5,677
  • 5
  • 31
  • 51
  • That of course prevents the web view from ever growing, but I don't know the length of the content beforehand, so fixing the height if the WebView in my case was not an option from the very start. – Nar Gar Feb 18 '12 at 09:48
  • By the way, I tried to simply load "http://m.yahoo.com" (quite a content-heavy and javascript-heavy page) - the page loads and the web view resizes to fit the content perfectly without any problems. So I am guessing there is a web view + jQuery error combination. – Nar Gar Feb 18 '12 at 09:53
  • 2
    Well, seems that ScrollView and WebView don't go together - period. I tried using the web view outside of the scroll view (which changed the structure of my dev templates quite a lot) - it worked. Still it does not explain the fact that the infinite growing happens only in pages that have jQuery – Nar Gar Feb 19 '12 at 23:41
  • 1
    You should not use ScrollView while using WebView. WebView already has ScrollView. – Hardik Trivedi Feb 20 '12 at 09:50
  • That's true Hardik, however if the webview is not the only element in the view, I don't have much of a choice except to use scroll view, in which case I need the webview to expand and wrap its own content. – Nar Gar Feb 21 '12 at 11:17