WebView in ScrollView: "View too large to fit into drawing cache" - how to rework layout?
Asked Answered
D

5

12

I have a layout with a ScrollView, which contains the following views: ImageView, TextView, WebView, TextView. (This is because I would like to scroll the whole together, not just the contents of the WebView)

After loading some HTML in the WebView, I receive the following:

WARN/View(632): View too large to fit into drawing cache, needs 14236800 bytes, only 1536000 available

...and the content of the WebView won't display. After removing the ScrollView, the warning disappears and all is well, except that I lose the wanted scroll functionality.

First: I know that trying to use a ScrollView inside another ScrollView is a bad thing in general, but I'm not 100% sure that in every case there's an equivalent solution without using ScrollView... I mean, of course one could put contents of ImageViews and TextViews into the WebView, but what about Buttons or any other UI elements needing interaction? Is there a way in general which could solve issues like this without giving up the layout and scrolling everything at once?

I've found out that I'm not the only one with this issue. For other examples, check those questions - without working solution yet:

Discommode answered 27/8, 2013 at 16:43 Comment(2)
try the stuff in this answer: https://mcmap.net/q/57356/-disable-scrolling-in-webview to disable scrolling on the WebView. That might improve the situation. That is somewhat of a shot in the dark on my part though.Skepful
I had this problem when I inadvertently put one scrollable view inside another scrollable view/container. The solution was to change the container to a LinearLayout. For more details, see my comment below this answer: https://mcmap.net/q/925576/-webview-in-scrollview-quot-view-too-large-to-fit-into-drawing-cache-quot-how-to-rework-layoutCalabash
W
17

The problem appears to be associated with hardware acceleration which is enabled by default if the API level is >= 14.

I have an app with a ScrollView that contains a number of views that I want to scroll as a single unit similar to the original poster - one of those views is a web view that wraps its content. If hardware acceleration is on and the rendered WebView is at all complex (images, borders, etc) then the drawing cache error message can be seen in LogCat. Some screen with 12 items worked while the next screen with 13 items did not work. I don't think that its the number of items that makes a difference, but the complexity of the final rendered screen.

The symptoms are typically a blank WebView - the other views are visually present and full formed. Very occasionally I see the whole screen blank, but that may have been while I was futzing with various suggestions found here on SO.

The message is not always seen. For example, I see the issue on a Samsung Galaxy 4 Mini running 4.2.2, while on other 4.x devices such as my cheap Chinese Samsung S3 clone running 4.1.2 everything is fine. I've not seen it on any 1.x or 2.x devices.

I tried to selectively turn off hardware acceleration on various views and layouts in the view hierarchy but in the end just turned of hardware acceleration for the whole app in the manifest file out of frustration and seeing how many hours I've wasted tracking this down.

After turning off hardware acceleration all issues have gone away. I see no noticeable performance differences on any of my devices. Presumably the 1.x and 2.x devices never used hardware acceleration in the first place, and my 4.x devices must be fast enough to cope with software only rendering. Not that my screens are that complicated.

Update APRIL 2015

Unfortunately the warning message is back on the Samsung Galaxy 4 Mini now running 4.4.2 even with hardware acceleration turned off. I have a webview with a JavaScript panel open/close animation. Everything works fine except that the initial layout (panel open) raises these warnings, and everytime I close or open the panel I also get them. These warnings are now just annoying, the app works fine.

Waksman answered 15/1, 2014 at 18:6 Comment(4)
Works for me with android:hardwareAccelerated="false".Thanks.Dutiful
yes but if you do android:hardwareAccelerated="false" then the embedded html5 videos won't play in the webview.........Endothecium
it happen for me when I change the Layer Type of the webview to use a software layer (default is hardware): mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);Luwian
for anyone facing issues like @HugoGresse , i faced a similar issue with webviews and recycler view. i had to turn on software layer in both webviews and in the recycler view and everything works fine after thatTopminnow
O
4

webView.setLayerType(WebView.LAYER_TYPE_NONE, null);

worked for me. Depending on the hardware, there is no memory left for an additional off-screen buffer as the WebView is rendered entirely when embedded in another scroll view.

I consider it a bug in Android that it is not automatically falling back to this (slower, but working) option.

Originative answered 10/2, 2016 at 9:8 Comment(0)
O
3

As the other answers stated, the issue appears when hardware acceleration is active. But turning off hardware acceleration for the whole app was no solution for me.

I figured I could solve the problem by setting android:layerType="software" to the ScrollView, which just disables the hardware acceleration for the ScrollView and its contents.

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layerType="software">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            ...
        </FrameLayout>

        <WebView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

Note that this may have negative performance implications, since rendering in software is usually slower.

Octahedral answered 24/5, 2016 at 16:9 Comment(1)
Have upvoted as this answer worked for me, but performance didn't seem great. I then realised that something must be wrong for me to get the View too large to fit into drawing cache warning in the first place... Then it dawned on me that I have a ListView (which is scrollable anyway) in a NestedScrollView. So I changed the NestedScrollView to a LinearLayout and now it works perfectly - without any need for android:layerType="software".Calabash
P
-2

I created something similar and all I needed was to add this to my WebView:

android:layout_height="match_parent"
Preceptory answered 27/8, 2013 at 17:38 Comment(1)
Sorry, but I don't think it would solve the problem with the drawing cache; in fact, I'm already using this in my problematic version.Discommode
M
-3

This works for me:

<WebView
        android:id="@+id/wv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal"/>
Massenet answered 18/11, 2013 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.