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?

andreas
  • 7,844
  • 9
  • 51
  • 72
  • Can you show the screenshot of the bottom of the page where you are facing problem. – Lalit Poptani Aug 11 '11 at 13:43
  • 1
    Check this [Link][1]. Try this. These may hep you. [1]: http://stackoverflow.com/questions/2559273/android-adjust-screen-when-keyboard-pops-up – Venky Aug 11 '11 at 13:47
  • 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? – andreas Aug 11 '11 at 14:19
  • Here is the solution for full screen mode: http://stackoverflow.com/a/19494006/3248593 – Morteza Rastgoo Apr 08 '15 at 10:49

8 Answers8

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.

andreas
  • 7,844
  • 9
  • 51
  • 72
  • I face the same problem. Is it possible to set the tag for all views programatically? – Display name Jul 10 '12 at 14:42
  • 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 Aug 22 '13 at 05:56
  • 3
    Hi @andreas, tried to follow your and Sandro's idea, so I created `` 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. – George Sep 03 '13 at 03:25
  • @congliu can you found any solution same issue i had – PankajAndroid Oct 15 '13 at 07:50
  • Hi @AndroidDev my bug got solved. The cause was the layout height of a parent view gets set to a fixed number. – George Oct 15 '13 at 07:57
  • 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 ? – PankajAndroid Oct 15 '13 at 08:53
  • 1
    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. – vbullinger Nov 14 '13 at 14:47
  • @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*? – Anas Azeem Nov 21 '13 at 06:11
  • @AnasAzeem you have to add the line quoted in the answer to every view in your XML layout. – howettl Nov 21 '13 at 17:54
  • but it is creating a white empty keyboard layout after a touch on webview. Like this mentioned here http://stackoverflow.com/questions/20559963/empty-space-when-i-return-to-activity-soft-keyboard-forced – Piyush Agarwal Aug 04 '15 at 08:42
  • "adjustResize" did not work for me, I had to use "adjustPan" – Facundo Olano Aug 04 '15 at 19:13
  • 4
    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"`. – Juan José Melero Gómez Nov 27 '15 at 09:11
  • 1
    In addition to full screen mode, setting the status bar to translucent (`android:windowTranslucentStatus = true`) will also prevent `adjustResize` from working. – geoffliu Apr 01 '16 at 08:29
  • `android:windowTranslucentStatus = true` was the missing piece – Francis Nepomuceno Sep 27 '18 at 14:48
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>
duggu
  • 37,851
  • 12
  • 116
  • 113
Sandro
  • 701
  • 6
  • 12
  • 6
    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? – CiscoIPPhone Feb 26 '13 at 17:32
  • 2
    yeah I also need my app to be fullscreen. Do you have a solution for this? – jayellos Apr 05 '13 at 09:51
  • 1
    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. – Sandro Apr 10 '13 at 12:09
  • I have same problem. I want my app full screen. – Shirish Herwade Aug 22 '13 at 06:00
  • 5
    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. – Gady Aug 11 '15 at 01:04
  • @CiscoIPPhone @yayellos Did you find a solution how to have the app in FullScreen and use also `android:windowSoftInputMode="adjustResize"` ? – Johny May 09 '17 at 09:53
  • I wanted fullscreen, and as @Gady says just use > android:theme="@android:style/Theme.NoTitleBar" and android:windowSoftInputMode="adjustResize" worked !!! Thanks... – Mikeys4u Aug 09 '17 at 16:29
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);
                    }
                }
            }
        });
    }
SiuFay
  • 333
  • 3
  • 8
  • 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\"}) – Daniel Silva Sep 25 '19 at 20:23
9

This would work:

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

Tsasaa
  • 111
  • 1
  • 1
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!

surajitk
  • 147
  • 4
  • 21
2

Beware that apart from the suggest answers

android:windowSoftInputMode="adjustResize"

Is not working when you are in immersive mode

unlimited101
  • 3,653
  • 4
  • 22
  • 41
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.
Pawel
  • 21
  • 1
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);

}

Denis Solakovic
  • 245
  • 3
  • 12