Textbox hidden below keyboard in Android webview
Solution 1
This is how I solved the problem. As Venky said, you have to add
android:windowSoftInputMode="adjustResize"
to your tag in the AndroidManifest.xml file. But in our case, it wasn't enough. Make sure you do this as well with your views, webviews etc. Then we finally made it work.
Solution 2
I was getting crazy nothing works android:windowSoftInputMode="adjustResize"
may help but be sure to have your app not in full screen.
Removing full screen for my app solved the problem with the layout resize with softkeyboard.
<item name="android:windowFullscreen">false</item>
Solution 3
For activities in full screen mode, android:windowSoftInputMode="adjustResize" will not work.
https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_FULLSCREEN
A fullscreen window will ignore a value of SOFT_INPUT_ADJUST_RESIZE for the window's softInputMode field; the window will stay fullscreen and will not resize.
I use the following method in the activity to resize the layout by setting a bottom padding:
public void adjustResizeOnGlobalLayout(@IdRes final int viewGroupId, final WebView webView) {
final View decorView = getWindow().getDecorView();
final ViewGroup viewGroup = (ViewGroup) findViewById(viewGroupId);
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
int paddingBottom = displayMetrics.heightPixels - rect.bottom;
if (viewGroup.getPaddingBottom() != paddingBottom) {
// showing/hiding the soft keyboard
viewGroup.setPadding(viewGroup.getPaddingLeft(), viewGroup.getPaddingTop(), viewGroup.getPaddingRight(), paddingBottom);
} else {
// soft keyboard shown/hidden and padding changed
if (paddingBottom != 0) {
// soft keyboard shown, scroll active element into view in case it is blocked by the soft keyboard
webView.evaluateJavascript("if (document.activeElement) { document.activeElement.scrollIntoView({behavior: \"smooth\", block: \"center\", inline: \"nearest\"}); }", null);
}
}
}
});
}
Solution 4
This would work:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Solution 5
Few things I learnt while solving this issue --- 1. Theme style should not contain Fullscreen True 2. Add android:windowSoftInputMode="adjustResize" 3. Remove android:scrollbars="none" is any.. . Cheers!
andreas
Updated on May 23, 2020Comments
-
andreas almost 4 years
I have created a simple iPhone/Android app, containing a normal webview. This webview calls my website.
On my website there are several forms with input type=text or textarea. I have a problem with those when they are at the bottom of the page!
1) In my iPhone app, the keyboard will automatically appear and push the textbox to the visible area of the phone screen. So there is nothing to do.
2) But in my Android app the textbox will stay at the same place and is eventually hidden by my keyboard. So the only option users have is to type "blind".
How can I fix this? Did anyone else meet this problem?
-
Display name almost 12 yearsI face the same problem. Is it possible to set the tag for all views programatically?
-
CiscoIPPhone about 11 yearsI need my app to be fullscreen - is there any way to have it fullscreen (i.e. no title/alerts bar) and have the keyboard not cover fields?
-
jayellos about 11 yearsyeah I also need my app to be fullscreen. Do you have a solution for this?
-
Sandro about 11 yearsNo I am sorry, after too many lost time with this problem I choose to not use a full screen app, if you find any kind of solution please add yours.
-
Shirish Herwade over 10 yearsI had the same problem and above solution can solve my problem. But my webview gets shrinked which I don't want. So I have to put windowSoftInputMode="adjustPan". And now i'm back to square 1. So can anyone please help
-
Shirish Herwade over 10 yearsI have same problem. I want my app full screen.
-
George over 10 yearsHi @andreas, tried to follow your and Sandro's idea, so I created
<style name="StyleWindowSoftInputModeAdjustResize" > <item name="android:windowSoftInputMode">adjustResize</item> <item name="android:windowFullscreen">false</item> </style>
and apply it to my webviewWebView webview = new WebView(this, null, R.style.StyleWindowSoftInputModeAdjustResize);
but soft keyboard is not showing when I click on some input box inside the a page loaded in the WebView... My WebView is inside a HorizontalScrollView, which is inside a vertical ScrollView. -
PankajAndroid over 10 years@congliu can you found any solution same issue i had
-
George over 10 yearsHi @AndroidDev my bug got solved. The cause was the layout height of a parent view gets set to a fixed number.
-
PankajAndroid over 10 yearsok but i had not other layout i had just webview and it will not display keybord in 2.2 only other higher version it's work fine can know about it ?
-
vbullinger over 10 yearsThis sounds like it can work for Android development. But what about Appcelerator Titanium development? There is a place for this kind of thing, but it only works for an Android app, not a mobile web app (I tried). In fact, it gives me an error because the namespace for this value is not available for a mobile website :/ I'd understand if you don't have any experience in Titanium development and don't know the answer.
-
Anas Azeem over 10 years@howettl: I am a little confused about "key here is to add the tag to all of the views". Can you please tell me how to do this. You please explain what does it mean to add the tag to all of the views?
-
howettl over 10 years@AnasAzeem you have to add the line quoted in the answer to every view in your XML layout.
-
Piyush Agarwal almost 9 yearsbut it is creating a white empty keyboard layout after a touch on webview. Like this mentioned here stackoverflow.com/questions/20559963/…
-
Facundo Olano almost 9 years"adjustResize" did not work for me, I had to use "adjustPan"
-
Gady over 8 yearsI also had
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
which preventedadjustResize
from working. Using justandroid:theme="@android:style/Theme.NoTitleBar"
madeadjustResize
work. -
Juan José Melero Gómez over 8 yearsPlease, note that
windowSoftInputMode
won't work if you usewindowFullscreen="true"
orandroid:theme="some style containing windowFullScreen=true"
, such asandroid:theme="@android:style/Theme.NoTitleBar.Fullscreen"
. -
geoffliu about 8 yearsIn addition to full screen mode, setting the status bar to translucent (
android:windowTranslucentStatus = true
) will also preventadjustResize
from working. -
Johny almost 7 years@CiscoIPPhone @yayellos Did you find a solution how to have the app in FullScreen and use also
android:windowSoftInputMode="adjustResize"
? -
Mikeys4u over 6 yearsI wanted fullscreen, and as @Gady says just use > android:theme="@android:style/Theme.NoTitleBar" and android:windowSoftInputMode="adjustResize" worked !!! Thanks...
-
Francis Nepomuceno over 5 years
android:windowTranslucentStatus = true
was the missing piece -
Daniel Silva over 4 yearsIt worked for me using the javascript in this answer, I ran it in a different way, but worked like a charm! document.activeElement.scrollIntoView({behavior: \"smooth\", block: \"center\", inline: \"nearest\"})