Textbox hidden below keyboard in Android webview
Asked Answered
I

8

62

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?

Ipswich answered 11/8, 2011 at 13:38 Comment(4)
Can you show the screenshot of the bottom of the page where you are facing problem.Eaddy
Check this [Link][1]. Try this. These may hep you. [1]: #2559773Sub
Thanks for your answer. In AndroidManifest.xml I have added >android:windowSoftInputMode="adjustResize"< right after >android:screenOrientation="portrait"<. I guess this is the correct place to put it? But it didn't work. Is it also depending on my webview, should I edit something there too in the xml?Ipswich
Here is the solution for full screen mode: https://mcmap.net/q/36361/-android-how-to-adjust-layout-in-full-screen-mode-when-softkeyboard-is-visibleNoise
I
55

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.

Ipswich answered 24/8, 2011 at 23:44 Comment(14)
I face the same problem. Is it possible to set the tag for all views programatically?Property
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 helpHiga
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.Washout
@congliu can you found any solution same issue i hadDowntime
Hi @AndroidDev my bug got solved. The cause was the layout height of a parent view gets set to a fixed number.Washout
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 ?Downtime
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.Unopened
@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?Orphism
@AnasAzeem you have to add the line quoted in the answer to every view in your XML layout.Opinion
but it is creating a white empty keyboard layout after a touch on webview. Like this mentioned here #20560463Patrickpatrilateral
"adjustResize" did not work for me, I had to use "adjustPan"Burnell
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".Micropathology
In addition to full screen mode, setting the status bar to translucent (android:windowTranslucentStatus = true) will also prevent adjustResize from working.Paralyze
android:windowTranslucentStatus = true was the missing pieceDisciple
A
28

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>
Amboina answered 28/1, 2013 at 15:54 Comment(7)
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?Demonstrative
yeah I also need my app to be fullscreen. Do you have a solution for this?Kiernan
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.Amboina
I have same problem. I want my app full screen.Higa
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.Ferdy
@Demonstrative @yayellos Did you find a solution how to have the app in FullScreen and use also android:windowSoftInputMode="adjustResize" ?Concuss
I wanted fullscreen, and as @Ferdy says just use > android:theme="@android:style/Theme.NoTitleBar" and android:windowSoftInputMode="adjustResize" worked !!! Thanks...Lutherlutheran
I
11

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);
                    }
                }
            }
        });
    }
Iminourea answered 23/5, 2019 at 6:26 Comment(1)
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\"})Rouen
C
9

This would work:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Cardio answered 14/9, 2016 at 15:54 Comment(0)
F
3

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!

Fixity answered 8/9, 2017 at 16:40 Comment(0)
K
2

Beware that apart from the suggest answers

android:windowSoftInputMode="adjustResize"

Is not working when you are in immersive mode

Knap answered 24/1, 2018 at 12:48 Comment(0)
R
2

In my case the succes achieved by:

  1. Adding below to manifest, webview and fragment:

    android:windowSoftInputMode="adjustResize"
    
  2. Using NON FullScreen Theme such as below:

    <style name="AppTheme" parent="android:Theme.Black.NoTitleBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowFullscreen">false</item>
    </style>
    
  3. NOT using ScrollView over WebView.
Revocation answered 26/1, 2018 at 5:42 Comment(0)
D
0

Yeah, had the same problem working with Webview, mine was with input filed on modal. Textfield didn't "focus" above the keyboard. The solution was to delay the function call. Hope someone finds this usefull.

   $("body").on("click", ".jstree-search-input", function () {  

    setTimeout(function(){ 
        androidScroll(); 
    }, 500);
    });

As you can see it's used for jstree input...

   function androidScroll() {
    // Webview focus call (pushes the modal over keyboard)
        $('.control-sidebar-open ').scrollTop($('.control-sidebar-open ')[0].scrollHeight);

}

Deen answered 11/5, 2016 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.