Android How to adjust layout in Full Screen Mode when softkeyboard is visible

前端 未结 25 1775
后悔当初
后悔当初 2020-11-22 03:56

I have researched a lot to adjust the layout when softkeyboard is active and I have successfully implemented it but the problem comes when I use android:theme=\"@andro

相关标签:
25条回答
  • 2020-11-22 04:54

    I tried many solutions include Joseph Johnson's and Johan Stuyts's. But as a result I got a white space between content and keyboard on some devices (like Lenovo s820) in all cases. So I made some changes to their codes and finally got working solution.

    My idea based on adding margin to top of content when keyboard is showing.

    contentContainer.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
        int usableHeightNow = contentAreaOfWindowBounds.height();
    
        if (usableHeightNow != usableHeightPrevious) {
    
            int difference = usableHeightNow - usableHeightPrevious;
    
            if (difference < 0 && difference < -150) {
                keyboardShowed = true;
                rootViewLayout.topMargin -= difference + 30;
                rootViewLayout.bottomMargin += 30;
            }
            else if (difference < 0 && difference > -150){
                rootViewLayout.topMargin -= difference + 30;
            }
            else if (difference > 0 && difference > 150) {
                keyboardShowed = false;
                rootViewLayout.topMargin = 0;
                rootViewLayout.bottomMargin = 0;
            }
    
            rootView.requestLayout();
    
            Log.e("Bug Workaround", "Difference: " + difference);
    
            usableHeightPrevious = usableHeightNow;
    }
    

    As you can see, I add 30 px to difference because there is a small white space between top of the screen and content zone with margin. And I dont know whence it appears so I decided just make margins smaller and now it works exactly how I needed.

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