Android FullScreen Keyboard show hide issue






import android.app.Activity
import android.content.Context
import android.graphics.Rect
import android.view.View
import android.widget.FrameLayout
import android.view.ViewConfiguration


class AndroidBug5497Workaround private constructor(activity: Activity) {

private val mContext: Context
private val mChildOfContent: View
private var usableHeightPrevious: Int = 0
private val frameLayoutParams: FrameLayout.LayoutParams

init {
mContext = activity
val content = activity.findViewById<View>(android.R.id.content) as FrameLayout
mChildOfContent = content.getChildAt(0)
mChildOfContent.viewTreeObserver.addOnGlobalLayoutListener { possiblyResizeChildOfContent() }
frameLayoutParams = mChildOfContent.layoutParams as FrameLayout.LayoutParams
}

private fun possiblyResizeChildOfContent() {
val usableHeightNow = computeUsableHeight()
val navigationBarHeight = getNavigationBarHeight()
if (usableHeightNow != usableHeightPrevious) {
val usableHeightSansKeyboard = mChildOfContent.rootView.height
val heightDifference = usableHeightSansKeyboard - usableHeightNow
if (heightDifference > usableHeightSansKeyboard / 4) {
// keyboard probably just became visible
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference
} else {
// heightDifference == 0 >> navigationbar invisible
if (heightDifference == 0)
{
frameLayoutParams.height = usableHeightSansKeyboard
}
else
{
frameLayoutParams.height = usableHeightSansKeyboard - navigationBarHeight
}
}
mChildOfContent.requestLayout()
usableHeightPrevious = usableHeightNow
}
}

private fun computeUsableHeight(): Int {
val r = Rect()
mChildOfContent.getWindowVisibleDisplayFrame(r)
return r.bottom - r.top
}

private fun getNavigationBarHeight(): Int {
val hasMenuKey = ViewConfiguration.get(mContext).hasPermanentMenuKey()
val resourceId = mContext.resources.getIdentifier("navigation_bar_height", "dimen", "android")
return if (resourceId > 0 && !hasMenuKey) {
mContext.resources.getDimensionPixelSize(resourceId)
} else 0
}



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

}


+ Recent posts