Android soft keyboard hides inputs from CordovaWebView when in fullscreen

前端 未结 3 360
春和景丽
春和景丽 2020-12-31 18:41

I have a CordovaWebView that presents some html forms. When i focus on an input field, the Android\'s soft keyboard pops up and, for certain fields, according to their posit

相关标签:
3条回答
  • 2020-12-31 19:22

    Remove android:theme="@android:style/Theme.NoTitleBar.Fullscreen" from manifest, and add these 2 lines of code:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    

    Make sure your onCreate method looks like this:

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().addFlags(LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yourLayout);
    
            // Your code ...
    }
    

    And everything will work :D.

    0 讨论(0)
  • 2020-12-31 19:35

    In fact, it is a well know bug, as @user2493245 said. But I found a workaround, at least regarding my specific case.

    on WebView, just check for the coordinates for the View's visible area.

    final View activityRootView = this.root;
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            activityRootView.getWindowVisibleDisplayFrame(r);
    
            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            if(heightDiff != lastValue) {
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    appView.sendJavascript("onKeyBoardShow(" + r.bottom + ");");
                } else {
                    appView.sendJavascript("onKeyBoardHide();");
                }
                lastValue = heightDiff;
            }
         }
    });  
    

    As you can see, I send that information to the WebView. On HTML, I have this two methods to handle the issue:

    function onKeyBoardShow(bottom) {
        var diff = ($('input[type=text]:focus').offset().top - bottom) + 50;
        if(diff > 0) {
            $('body').css("top", (diff * -1) + "px");
        }
    };
    
    function onKeyBoardHide() {
        $('body').css("top", "0px");
    };
    

    Basically, onKeyBoardShow, it gets the input field focused and calculates the amount of pixels that will be necessary to move the body, allowing the user to see the input field. onKeyBoardHide, simply puts the body to its original position.

    PS: This only functions when the viewport targets devicedpi, as we need to modify the way we get the dif regarding the dpi of the device.

    PS2: First amount of code is not mine, I only edited to fill my needs. I saw that on a SO question, but unfortunatelly now i can't find it. If i find it, i'll post the link here.

    0 讨论(0)
  • 2020-12-31 19:48

    I have the same problem and i found out it is a well known bug.

    A workaround could be that u write a plugin that disables the fullscreen just before the softkeyboard pops up and reenables it afterwards.

    0 讨论(0)
提交回复
热议问题