I need an activity to load an HTML document into a WebView, but for the WebView to then be resized so it tightly wraps the HTML document. e.g. if the HTML document contains a single div which is 300×200 pixels in size, I’d want the WebView to resize to be 300px x 200px to exactly contain the document.
Vertically WebView behaves this way naturally. The height of a WebView (with wrap_content) is determined by the height of the HTML document, but oddly not the width which always fills the width (unless a dimension is specified for the width). i.e. as far as I can see using layout_width=”wrap_content” with a WebView doesn’t work.
Is there a way to configure WebView to resize its width in this way? I cannot see one. If the HTML content is thinner than the screen width, I want the WebView to be thinner.
If not, is there a way to acquire absolute element sizes post-rendering in the HTML document model, which I can then use to resize the WebView width?
To demonstrate, here’s the statement to load static content into the WebView:
browser.loadDataWithBaseURL("/", "Hello!", "text/html", "utf-8", null);
And here’s the simple layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView
android:id="@+id/webview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
/>
</LinearLayout>
I’d expect this to show a WebView to the upper left extending part way across the width, but instead it extends all the way across the whole width of the screen (despite wrap_content, and the content being smaller than the width)

I think, you might use android:layout_marginRight=200px feature of WebView in your layout xml.