Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Shrink WebView size dynamically according to its content?

A detailed explanation of issue can be understood from this question as the user had added image to explain it better.

I am loading Html content in my WebView. My layout is having many view and WebView is place inside ScrollView(as per layout requirement). Please don't answer as - "Don't put WebView inside ScrollView". I know that its not a good thing to put WebView inside a ScrollView, but as per requirement I need to do so.

So, I have Left Fragment(showing List Items) and Right Fragment(Showing data reflected on selection of List Item from Left Fragment). Now, first of all when I load Html content inside WebV it shows correct. After that when I refresh WebView with new Html content the problem occurs.

Suppose, my first Html content is of 100 lines it shows correctly and then I reload WebView with my new Html content which is of 40 lines then the WebView is not shrinking and fitting to the content with 40 lines, it still remains as long as 100 lines showing white/blank space at the bottom.

So, it seems that WebView is able to re-size itself from less content which is previously loaded to more content but unable to re-size itself when the content is less than previously loaded content.

I had tried many ways,

  • Adding android:hardwareAccelerated="true" in Manifest
  • This Blog
  • Also many other ways and blog
  • Also I had tried to use mWebView.clearView(); which causes to re-size the size of WebView but at times the WebView start blinking which is just annoying. Similar to this video

But, couldn't find any proper solution. If anyone of you have the same issue before just let me know the best solution I could apply.

UPDATE -

After further googling it seems that this is a well-known issue in Honeycomb. This question also indicates the similar issue.

like image 695
Lalit Poptani Avatar asked Mar 21 '13 11:03

Lalit Poptani


1 Answers

Seems there's no way to resize the WebView(decrease) its size dynamically once its loaded. So, the only thing that could solve my problem was reloading the complete WebView.

So, instead of defining the Fragment in XML, I changed my stuff by adding Fragment dynamically every time. By which I was able to solve my issue.

like image 86
Lalit Poptani Avatar answered Oct 24 '22 18:10

Lalit Poptani