Advertisement
sanya5791

Untitled

Feb 26th, 2021
1,344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.32 KB | None | 0 0
  1. package ca.foundrybc.foundryapp.presentation.util.extension
  2.  
  3. import android.app.Activity
  4. import android.graphics.Rect
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.view.Window
  8. import androidx.annotation.StringRes
  9. import com.google.android.material.snackbar.Snackbar
  10.  
  11. fun Activity.showBottomSnackBar(@StringRes message: Int) {
  12.     val view: View = findViewById(android.R.id.content)
  13.     Snackbar.make(view, message, Snackbar.LENGTH_LONG)
  14.         .show()
  15. }
  16.  
  17. fun Activity.getRootView(): View = this.findViewById(Window.ID_ANDROID_CONTENT)
  18.  
  19. fun Activity.getRootViewGroup(): ViewGroup = getRootView() as ViewGroup
  20.  
  21. fun Activity.getViewBindingView(): View = getRootViewGroup().getChildAt(0)
  22.  
  23. private const val KEYBOARD_MIN_HEIGHT_RATIO = 0.15
  24.  
  25. fun Activity.isKeyboardOpen(): Boolean {
  26.     val contentRoot = getRootViewGroup()
  27.  
  28.     val activityRoot = contentRoot.getChildAt(0)
  29.  
  30.     val rect = Rect()
  31.     activityRoot.getWindowVisibleDisplayFrame(rect)
  32.  
  33.     val location = IntArray(2)
  34.     contentRoot.getLocationOnScreen(location)
  35.  
  36.     val screenHeight = activityRoot.rootView.height
  37.     val heightDiff = screenHeight - rect.height() - location[1]
  38.  
  39.     return heightDiff > screenHeight * KEYBOARD_MIN_HEIGHT_RATIO
  40. }
  41.  
  42. fun Activity.isKeyboardClosed(): Boolean {
  43.     return !this.isKeyboardOpen()
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement