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.
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.
This is a known problem with WebView
and some available solution doesn't work in all the devices.
I tried many solution given in SO answers and also searched over the internet but none of them worked for me too.
I ended up , removing the WebView and adding the WebView again at the same position in in Layout
Here is that demo code,
final RelativeLayout rl = new RelativeLayout(this);
myWebView = new WebView(this);
myWebView.setBackgroundColor(Color.GRAY);
myWebView.loadUrl("file:///android_asset/1.htm");
rl.addView(myWebView); // ******* Added At Index 0
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (!flg) {
rl.removeViewAt(0);
myWebView = new WebView(MainActivity.this);
myWebView.setBackgroundColor(Color.GRAY);
myWebView.loadUrl("file:///android_asset/3.htm");
rl.addView(myWebView, 0, new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)));
flg = true;
} else {
rl.removeViewAt(0);
myWebView = new WebView(MainActivity.this);
myWebView.setBackgroundColor(Color.GRAY);
myWebView.loadUrl("file:///android_asset/1.htm");
rl.addView(myWebView, 0, new RelativeLayout.LayoutParams(
new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)));
flg = false;
}
}
});
rl.addView(btn); // ******* Added At Index 1
setContentView(rl);
guys, it seems I found a solution for local content. You should just call webView.reload() before loading new content - somehow it does the resizing without any blinking.
i have not tried but why dont you give a try by setting weblayout params again, before u load the new contents. ie set height and width to wrapcontent. if success found please reply. if wont find success better go for javascipt
I set the visibility of my WebView to Gone, and load the content into the WebView while it's gone. Then I set the visibility to visible after letting the DOM and JS load and it seems to draw the length correctly. Since there isn't a callback like onPageFinished for determining when the JS has finished executing, I give the WebView a one second delay before I set the visibility to true. This workaround suffices for my purposes, so hopefully it addresses the needs of some of you guys!
Everytime creating an object of webview and give its param is a pain instead you can do following steps and it will work
Webview will shrink its height.
Make sure you are not putting any content in empty.html
This do the magic! It says
Android property: android:layout_weight = "1 in the layout should work...
In my case, WebView is in the RelativeLayout. So doing thi android:layout_height="wrap_content" worked like a charm :)
Here I have changed the height of webview depending of softkeyboard display
activityRootView = (RelativeLayout) findViewById(R.id.webview1);
mWebView = (WebViewEx) WebViewActivity.this.findViewById(R.id.webview);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if(heightDiff != lastValue) {
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
Log.i("Screen Size Changed","KeyBoard Displaying"+ r.bottom);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.height=r.bottom;
mWebView.setLayoutParams(params);
//appView.sendJavascript("onKeyBoardShow(" + r.bottom + ");");
} else {
/// appView.sendJavascript("onKeyBoardHide();");
Log.i("Screen Size Changed","KeyBoard not displaying" +r.bottom);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.height=r.bottom;
mWebView.setLayoutParams(params);
}
lastValue = heightDiff;
}
}
});
As @Ankit pointed out, go to an empty page could help. "about:blank" is better, though.