Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How a change the content size of a webview in android?

I have a webview control in a scrollview. when i load a long html document into it, it resizes to accommodate it. this is correct. however, when i use the same control again afterwards and put less space-consuming html into it, the content size doesn't shrink. this is a problem because the user is able to still scroll a short document into white space. any ideas on how to resolve this?

like image 542
blaine Avatar asked Dec 14 '10 15:12

blaine


People also ask

What is alternative of WebView in android?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.


2 Answers

I had a similar problem and resolved it by clearing the view and re-measuring with a small size prior to loading the view again.

e.g.

  webView.clearView();
  webView.measure(10, 10);
like image 130
SoftWyer Avatar answered Oct 13 '22 19:10

SoftWyer


This didn't work for me when using:

loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)

But calling requestLayout() on the webview after loading the data worked except for a strange glitch with the scrollbar flickering if changing the view while scrolling.

webView.clearView();
webView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
webView.requestLayout();
like image 4
dt0 Avatar answered Oct 13 '22 18:10

dt0