Textbox hidden below keyboard in Android webview

52,243

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!

Share:
52,243
andreas
Author by

andreas

Updated on May 23, 2020

Comments

  • andreas
    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
    Display name almost 12 years
    I face the same problem. Is it possible to set the tag for all views programatically?
  • CiscoIPPhone
    CiscoIPPhone about 11 years
    I 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
    jayellos about 11 years
    yeah I also need my app to be fullscreen. Do you have a solution for this?
  • Sandro
    Sandro about 11 years
    No 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
    Shirish Herwade over 10 years
    I 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
    Shirish Herwade over 10 years
    I have same problem. I want my app full screen.
  • George
    George over 10 years
    Hi @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 webview WebView 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
    PankajAndroid over 10 years
    @congliu can you found any solution same issue i had
  • George
    George over 10 years
    Hi @AndroidDev my bug got solved. The cause was the layout height of a parent view gets set to a fixed number.
  • PankajAndroid
    PankajAndroid over 10 years
    ok 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
    vbullinger over 10 years
    This 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
    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
    howettl over 10 years
    @AnasAzeem you have to add the line quoted in the answer to every view in your XML layout.
  • Piyush Agarwal
    Piyush Agarwal almost 9 years
    but it is creating a white empty keyboard layout after a touch on webview. Like this mentioned here stackoverflow.com/questions/20559963/…
  • Facundo Olano
    Facundo Olano almost 9 years
    "adjustResize" did not work for me, I had to use "adjustPan"
  • Gady
    Gady over 8 years
    I also had android:theme="@android:style/Theme.NoTitleBar.Fullscreen" which prevented adjustResize from working. Using just android:theme="@android:style/Theme.NoTitleBar" made adjustResize work.
  • Juan José Melero Gómez
    Juan José Melero Gómez over 8 years
    Please, note that windowSoftInputMode won't work if you use windowFullscreen="true" or android:theme="some style containing windowFullScreen=true", such as android:theme="@android:style/Theme.NoTitleBar.Fullscreen".
  • geoffliu
    geoffliu about 8 years
    In addition to full screen mode, setting the status bar to translucent (android:windowTranslucentStatus = true) will also prevent adjustResize from working.
  • Johny
    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
    Mikeys4u over 6 years
    I wanted fullscreen, and as @Gady says just use > android:theme="@android:style/Theme.NoTitleBar" and android:windowSoftInputMode="adjustResize" worked !!! Thanks...
  • Francis Nepomuceno
    Francis Nepomuceno over 5 years
    android:windowTranslucentStatus = true was the missing piece
  • Daniel Silva
    Daniel Silva over 4 years
    It 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\"})