Advertisement
developerandr

Fullscreen

May 6th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. open class FullScreen : AppCompatActivity() {
  2.  
  3.  
  4. override fun onBackPressed() {
  5. super.onBackPressed()
  6. val decorView = window.decorView
  7. decorView.systemUiVisibility = hideSystemUI()
  8. window.decorView.viewTreeObserver.addOnGlobalLayoutListener {
  9. val r = Rect()
  10. decorView.getWindowVisibleDisplayFrame(r)
  11. val screenHeight = decorView.rootView.height
  12. val keypadHeight = screenHeight - r!!.bottom
  13. decorView.isFocusableInTouchMode = true
  14. decorView.requestFocus()
  15.  
  16.  
  17. Log.d("keypadHeight", "keypadHeight = $keypadHeight, ${screenHeight * 0.15}")
  18. if (keypadHeight > screenHeight * 0.15) {
  19. showSystemUI()
  20. hideKeyboardFrom(this, window.decorView)
  21. } else {
  22.  
  23. onBackPressed()
  24. decorView.setOnSystemUiVisibilityChangeListener { visibility ->
  25. if (visibility == 0) {
  26. decorView.systemUiVisibility = hideSystemUI()
  27. }
  28. }
  29. }
  30. }
  31. }
  32.  
  33.  
  34. override fun onCreate(savedInstanceState: Bundle?) {
  35. super.onCreate(savedInstanceState)
  36. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  37.  
  38. fullScreenActivity()
  39.  
  40. }
  41.  
  42. fun fullScreenActivity() {
  43. val decorView = window.decorView
  44.  
  45. decorView.systemUiVisibility = hideSystemUI()
  46.  
  47. decorView.viewTreeObserver.addOnGlobalLayoutListener {
  48. val r = Rect()
  49. decorView.getWindowVisibleDisplayFrame(r)
  50. val screenHeight = decorView.rootView.height
  51.  
  52.  
  53. val keypadHeight = screenHeight - r!!.bottom
  54.  
  55. Log.d("keypadHeight", "keypadHeight = $keypadHeight, ${screenHeight * 0.15}")
  56. if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
  57.  
  58. decorView.systemUiVisibility = showSystemUI()
  59.  
  60.  
  61. } else {
  62.  
  63. decorView.systemUiVisibility = hideSystemUI()
  64.  
  65. }
  66. }
  67. }
  68.  
  69.  
  70.  
  71. fun showKeyboardFrom(context: Context, view: View) {
  72. val imm: InputMethodManager =
  73. context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
  74. imm.showSoftInputFromInputMethod(view.windowToken, 0)
  75. }
  76.  
  77. fun hideKeyboardFrom(context: Context, view: View) {
  78. val imm: InputMethodManager =
  79. context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
  80. imm.hideSoftInputFromWindow(view.windowToken, 0)
  81. }
  82.  
  83. fun showSystemUI(): Int {
  84. return (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  85. or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  86. or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
  87.  
  88. }
  89.  
  90. fun hideSystemUI(): Int {
  91. return (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
  92. or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
  93. or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
  94. or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  95. or View.SYSTEM_UI_FLAG_FULLSCREEN
  96. or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement