Created
April 18, 2019 07:17
-
-
Save umpteenthdev/7b518b4919365493741a40a01b261552 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import android.annotation.SuppressLint | |
| import android.graphics.Rect | |
| import android.os.Build | |
| import android.view.View | |
| import android.view.ViewTreeObserver | |
| import androidx.core.view.updatePaddingRelative | |
| class UnderKeyboardViewElevator(private val decorView: View, private val contentView: View) { | |
| private var initialPaddingBottom: Int = contentView.paddingBottom | |
| private var onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener { | |
| val r = Rect() | |
| decorView.getWindowVisibleDisplayFrame(r) | |
| val height = decorView.context.resources.displayMetrics.heightPixels | |
| val diff = height - r.bottom | |
| if (diff != 0) { | |
| val targetPadding = diff + initialPaddingBottom | |
| if (contentView.paddingBottom != targetPadding) { | |
| contentView.updatePaddingRelative(bottom = targetPadding) | |
| } | |
| } else { | |
| if (contentView.paddingBottom != initialPaddingBottom) { | |
| contentView.updatePaddingRelative(bottom = initialPaddingBottom) | |
| } | |
| } | |
| } | |
| init { | |
| //only required on newer android versions. it was working on API level 19 | |
| if (Build.VERSION.SDK_INT >= 19) { | |
| decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener) | |
| } | |
| } | |
| fun enable() { | |
| if (Build.VERSION.SDK_INT >= 19) { | |
| decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener) | |
| } | |
| } | |
| fun disable() { | |
| if (Build.VERSION.SDK_INT >= 19) { | |
| decorView.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment