Last active
November 30, 2023 13:47
-
-
Save bronze1man/8686b4811b8c9f042d9f9ea82ac6e86d to your computer and use it in GitHub Desktop.
Detecting if the Android keyboard is visible or hidden without @composable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// copy and change from https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/java/com/blankj/utilcode/util/KeyboardUtils.java#L185C1-L207C6 | |
private var sDecorViewDelta = 0 | |
fun isSoftInputVisible(activity: Activity): Boolean { | |
val window = activity.window | |
val decorView = window.decorView | |
val outRect = Rect() | |
decorView.getWindowVisibleDisplayFrame(outRect) | |
val delta = Math.abs(decorView.bottom - outRect.bottom) | |
var NavBarHeight = 0 | |
val res = activity.application.resources | |
val resId1 = res.getIdentifier("navigation_bar_height", "dimen", "android") | |
if (resId1!=0){ | |
NavBarHeight = res.getDimensionPixelSize(resId1) | |
} | |
var StatusBarHeight = 0 | |
val resId2 = res.getIdentifier("status_bar_height", "dimen", "android") | |
if (resId2!=0){ | |
StatusBarHeight = res.getDimensionPixelSize(resId2) | |
} | |
if (delta <= NavBarHeight + StatusBarHeight) { | |
sDecorViewDelta = delta | |
return false | |
} | |
return (delta - sDecorViewDelta)>0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment