3

My goal is to create a WebView with small bounds, and have it animate smoothly to a predetermined width and whatever height is necessary to contain its contents, to give the impression of it expanding to its full size.

I don't want the contents of the WebView to relayout repeatedly during the animation, so I tried setting the viewport width to the final width of the animation. I achieved this by adding

<meta name="viewport" contents="width=X" />

where X is my desired width. Additionally I used:

WebSettings settings = webView.getSettings();
settings.setUseWideViewPort(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);

to ensure that the viewport tag was used, and that my content would extend beyond the bounds of my webview.

This works (although only if I do not use wrap_content as the layout parameter of either of my WebView's dimensions). The issue is that when I extend the viewport beyond the width of my WebView, its content height is extended proportionally.

For example, if I create an empty project that launches an activity containing a WebView that is 350x200px, and then call:

webview.loadData("<html><head><meta name=\"viewport\" content=\"width=450\" /></head></html>");

the result will be an empty WebView with a width of 450 css pixels and a content height of 257 device pixels. The 257 seems to come from 200 * 450 / 350.

After experimentation, it seems that after loading a WebView with a viewport that extends beyond its width, the content height will always be the maximum of the height required to layout the content and the WebView's height multiplied by the viewport's width divided by the WebView's width.

Additionally, if after this layout occurs I increase the height of the WebView, the contentHeight will increase proportionally as well. So, at this point no matter how large I make my WebView it will always have an empty scrollable space below the content.

So, I have two problems. Firstly, after the layout has occurred, I cannot find the appropriate final height of my WebView, because content height now represents the height*(viewport_width/width) instead of the height the content lays out to. Secondly, even if I could determine the appropriate height of my WebView, if I set this height then the WebView will lay out to include even more empty space below my content.

I've tried setting the height attribute of the viewport in the metatag, this doesn't appear to have any effect. I'm assuming this issue is due to the WebView not wanting to change the aspect ratio of the viewport when it is scaled up. Does anyone know of any ways to get around this limitation? Is there any way to achieve horizontal scrolling without vertical scrolling?

Thanks for any help or recommendation!

groucho
  • 1,835
  • 1
  • 13
  • 15

0 Answers0