Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with the activity using dialog theme #68

Open
nguyencan opened this issue Mar 11, 2021 · 1 comment
Open

Not working with the activity using dialog theme #68

nguyencan opened this issue Mar 11, 2021 · 1 comment

Comments

@nguyencan
Copy link

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.

@Daphne-CoffeeIT
Copy link

Daphne-CoffeeIT commented Aug 8, 2022

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
        }
    })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants