mvs
mvs

Reputation: 11

Sometimes for the short period opened softkeyboard decreases the WindowVisibleDisplayFrame area more than it's needed for softkeyboard placement

I need the keyboard to cover the main content on the screen but some views should be placed above the keyboard.

So I use WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING for my activity and hack with a transparent popupWindow and OnGlobalLayoutListener to know when the keyboard opening event happens and the height of this keyboard. It works.

But sometimes the popupView.getWindowVisibleDisplayFrame(rect) from onGlobalLayout() method calculates height smaller than I expect (that means the keyboard is higher ( in 2 times) but you can't see it on the screen, it looks as usual ) and in a short time (20 - 60 ms, average 23 ms) is called new onGlobalLayout() method with correct WindowVisibleDisplayFrame value. I fix it with postDellay. But it is a hack.

And I want to know why it happens and what does it depend on, to make my code clearer.

PS I checked https://github.com/siebeprojects/samples-keyboardheight and in logs, I saw the same issue.

my code:

internal class SoftKeyboardChangeHeightHandler(
        val parentActivity: Activity,
        val viewsForTranslation: View,
        val keyboardChangeHeightListener: SoftKeyboardChangeHeightListener? = null
    ) : PopupWindow(parentActivity.applicationContext), ViewTreeObserver.OnGlobalLayoutListener {
        private val decorView = parentActivity.window.decorView
        private val popupView = View(parentActivity.applicationContext)
        private var portraitHeightMax = 0
        private var landscapeHeightMax = 0
        private var keyboardHeight = 0
    
        init {
            contentView = popupView
            setBackgroundDrawable(ColorDrawable(0))
            width = 0
            height = ViewGroup.LayoutParams.MATCH_PARENT
            softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
            inputMethodMode = INPUT_METHOD_NEEDED
        }
    
        fun onResume() {
            popupView.viewTreeObserver.addOnGlobalLayoutListener(this)
            if (!isShowing) {
                decorView.post { showAtLocation(decorView, Gravity.NO_GRAVITY, 0, 0) }
            }
        }
    
        fun onPause() {
            popupView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    
        override fun onGlobalLayout() {
            val rect = Rect()
            popupView.getWindowVisibleDisplayFrame(rect)
            keyboardHeight = if (getScreenOrientation() == Configuration.ORIENTATION_PORTRAIT) {
                if (rect.bottom > portraitHeightMax) {
                    portraitHeightMax = rect.bottom
                }
                portraitHeightMax - rect.bottom
            } else {
                if (rect.bottom > landscapeHeightMax) {
                    landscapeHeightMax = rect.bottom
                }
                landscapeHeightMax - rect.bottom
            }
            val screenSize = Point()
            parentActivity.windowManager.defaultDisplay.getSize(screenSize)
            L.info {
                "keyboardHeight = $keyboardHeight" +
                    "\nvisibleDisplay.bottom = ${rect.bottom}" +
                    "\nheightMax = $portraitHeightMax\ndecorViewHeight = ${decorView.height} " +
                    "\nscreensizeY = ${screenSize.y}"
            }
    
            val callBack = {
                keyboardChangeHeightListener
                viewsForTranslation.translationY = -keyboardHeight.toFloat()
            }
            viewsForTranslation.removeCallbacks(callBack)
            viewsForTranslation.postDelayed(callBack, 100)
        }
    }

logs:

2021-08-19 15:18:33.021 SoftKeyboardChangeHeightHandler:  keyboardHeight = 685 // open
        visibleDisplay.bottom = 1403
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088 

2021-08-19 15:18:34.134 SoftKeyboardChangeHeightHandler:  keyboardHeight = 0 //close correct size
        visibleDisplay.bottom = 2088
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088
   
 2021-08-19 15:18:34.762 SoftKeyboardChangeHeightHandler:  keyboardHeight = 1370 // open with size *2
        visibleDisplay.bottom = 718
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088

 2021-08-19 15:18:34.781 SoftKeyboardChangeHeightHandler:  keyboardHeight = 685 //correct size
        visibleDisplay.bottom = 1403
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088

 2021-08-19 15:18:38.513 SoftKeyboardChangeHeightHandler:  keyboardHeight = 0
        visibleDisplay.bottom = 2088
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088

 2021-08-19 15:18:38.535 SoftKeyboardChangeHeightHandler:  keyboardHeight = 0
        visibleDisplay.bottom = 2088
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088 2021-08-19

 15:18:42.049 SoftKeyboardChangeHeightHandler:  keyboardHeight = 685
        visibleDisplay.bottom = 1403
        heightMax = 2088
        decorViewHeight = 2220
        screensizeY = 2088

Upvotes: 1

Views: 42

Answers (0)

Related Questions