Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView infinitely expands - how can this be prevented?

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.

like image 781
Nar Gar Avatar asked Feb 18 '12 03:02

Nar Gar


People also ask

How do you prevent the WebView from invoking the device's Web browser when a redirection occurs in the WebView?

To detect and intercept any redirection from WebView , we can use shouldOverrideUrlLoading and return true if it is supported to redirect into native page so that WebView stop the URL redirection in the web page and stay in the current page.

How do I close WebView on Android?

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


1 Answers

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;
}
like image 177
Nachi Avatar answered Oct 02 '22 05:10

Nachi