Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView doesn't resize

I've developed a web application for Firefox OS, but I wanted to make it run "natively" in Android. My application consists of a dialogue box that is a div which fills the entire page when visible, which I've tested using the Firefox responsive design tool and it does resize correctly. When a soft keyboard appears, I would want the dialogue to resize to the viewport left over as there are some buttons at the bottom of the dialogue.

I created an application with a single activity that contains just a WebView. I wanted to the view to resize when the keyboard appears, so I used:

android:windowSoftInputMode="stateUnspecified|adjustResize"

which didn't seem to make any difference to the WebView when the keyboard was visible. I looked online and there were many people complaining about this problem, but they all had the same thing in common, they were using the Theme.Black.NoTitleBar.Fullscreen theme, which I am not. I tried all of the solutions to that problem, but none of them worked.

I then began to wonder if there was a bug in WebView which meant it didn't resize. So, I wrote a custom View which printed the dimensions in onLayout.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    Log.d(TAG, t + " " + r + " " + b + " " + l);
}

And sure enough, the view was being resized correctly when the keyboard appeared and disappared. So, I did some more research and I found a few answers that said that you need to hide the WebView, reload it, and then show it again.

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    Log.d(TAG, t + " " + r + " " + b + " " + l);

    mWebView.setVisibility(View.GONE);
    mWebView.layout(l, t, r, b);
    mWebView.invalidate();
    mWebView.reload();
    mWebView.setVisibility(View.VISIBLE);
}

Again, this did not solve my problem. (And as a side point, this is a web-app with a single .html file and JS causes the state of all the elements in the app to change in runtime, so reloading it is not an option.)

My question is whether anyone knows of a way of resizing a WebView once the content has been loaded when the keyboard is visible.

like image 629
Tom Leese Avatar asked Aug 19 '13 10:08

Tom Leese


2 Answers

That's not exactly the answer to your question, but you will also notice that WebView does re-size when it's about getting bigger, but doesn't reduce when the content's height decreases, as I have already answered on this question: WebView height = wrap_content with changing font size doesn't work

The bug/feature is referenced on Google's issue tracker: https://code.google.com/p/android/issues/detail?id=18726

Please don't blame me, I just wanted to point that for further problems you could encounter with WebView.

like image 116
Thibault D. Avatar answered Nov 27 '22 23:11

Thibault D.


The problem turned out to be that I was using position: absolute inside a position: fixed div. The outer div was resizing properly, but the inner one was not. The reason I had to use position: absolute was because there is a bug in WebKit which means that you cannot transform fixed positioned divs.

like image 27
Tom Leese Avatar answered Nov 28 '22 01:11

Tom Leese