Skip to content

Instantly share code, notes, and snippets.

@kibotu
Created March 12, 2025 10:37
Show Gist options
  • Save kibotu/d6a9302a1292ff6e6887f12f675df33b to your computer and use it in GitHub Desktop.
Save kibotu/d6a9302a1292ff6e6887f12f675df33b to your computer and use it in GitHub Desktop.
import android.R
import android.app.Activity
import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.widget.FrameLayout
class AndroidBug5497Workaround private constructor(activity: Activity) {
private val mChildOfContent: View
private var usableHeightPrevious = 0
private val frameLayoutParams: FrameLayout.LayoutParams
init {
val content = activity.findViewById<View?>(R.id.content) as FrameLayout
mChildOfContent = content.getChildAt(0)
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
possiblyResizeChildOfContent()
}
})
frameLayoutParams = mChildOfContent.layoutParams as FrameLayout.LayoutParams
}
private fun possiblyResizeChildOfContent() {
val usableHeightNow = computeUsableHeight()
if (usableHeightNow != usableHeightPrevious) {
val usableHeightSansKeyboard = mChildOfContent.getRootView().height
val heightDifference = usableHeightSansKeyboard - usableHeightNow
if (heightDifference > (usableHeightSansKeyboard / 4)) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference
} else {
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightSansKeyboard
}
mChildOfContent.requestLayout()
usableHeightPrevious = usableHeightNow
}
}
private fun computeUsableHeight(): Int {
val r = Rect()
mChildOfContent.getWindowVisibleDisplayFrame(r)
return (r.bottom - r.top)
}
companion object {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
fun assistActivity(activity: Activity) {
AndroidBug5497Workaround(activity)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment