KeyboardVisibilityEvent icon indicating copy to clipboard operation
KeyboardVisibilityEvent copied to clipboard

Not working with the activity using dialog theme

Open nguyencan opened this issue 3 years ago • 1 comments

I have activity A (normal theme) register keyboard listener event. From A, I start activity B (dialog theme & register keyboard listener event too), when keyboard changed, I only receive the event on A, not on B.

Can you help me? I want to receive the event on activity B.

nguyencan avatar Mar 11 '21 04:03 nguyencan

I managed to make it work in a Dialog with the following code, unfortunately bypassing the KeyboardVisibilityEvent library:

private fun setKeyboardListener() {
    // Top-level window decor view.
    val decorView = activity?.window?.decorView
    // Register global layout listener.
    decorView?.viewTreeObserver?.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        // Retrieve visible rectangle inside window.
        private val windowVisibleDisplayFrame: Rect = Rect()
        private var lastVisibleDecorViewHeight = 0
        override fun onGlobalLayout() {
            decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
            val visibleDecorViewHeight: Int = windowVisibleDisplayFrame.height()
            if (lastVisibleDecorViewHeight != 0) {
                if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
                    binding.keyboardIsOpen = true
                } else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
                    binding.keyboardIsOpen = false
                }
            }
            // Save current decor view height for the next call.
            lastVisibleDecorViewHeight = visibleDecorViewHeight
        }
    })
}

Daphne-CoffeeIT avatar Aug 08 '22 13:08 Daphne-CoffeeIT