Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open class FullScreen : AppCompatActivity() {
- override fun onBackPressed() {
- super.onBackPressed()
- val decorView = window.decorView
- decorView.systemUiVisibility = hideSystemUI()
- window.decorView.viewTreeObserver.addOnGlobalLayoutListener {
- val r = Rect()
- decorView.getWindowVisibleDisplayFrame(r)
- val screenHeight = decorView.rootView.height
- val keypadHeight = screenHeight - r!!.bottom
- decorView.isFocusableInTouchMode = true
- decorView.requestFocus()
- Log.d("keypadHeight", "keypadHeight = $keypadHeight, ${screenHeight * 0.15}")
- if (keypadHeight > screenHeight * 0.15) {
- showSystemUI()
- hideKeyboardFrom(this, window.decorView)
- } else {
- onBackPressed()
- decorView.setOnSystemUiVisibilityChangeListener { visibility ->
- if (visibility == 0) {
- decorView.systemUiVisibility = hideSystemUI()
- }
- }
- }
- }
- }
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
- fullScreenActivity()
- }
- fun fullScreenActivity() {
- val decorView = window.decorView
- decorView.systemUiVisibility = hideSystemUI()
- decorView.viewTreeObserver.addOnGlobalLayoutListener {
- val r = Rect()
- decorView.getWindowVisibleDisplayFrame(r)
- val screenHeight = decorView.rootView.height
- val keypadHeight = screenHeight - r!!.bottom
- Log.d("keypadHeight", "keypadHeight = $keypadHeight, ${screenHeight * 0.15}")
- if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
- decorView.systemUiVisibility = showSystemUI()
- } else {
- decorView.systemUiVisibility = hideSystemUI()
- }
- }
- }
- fun showKeyboardFrom(context: Context, view: View) {
- val imm: InputMethodManager =
- context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
- imm.showSoftInputFromInputMethod(view.windowToken, 0)
- }
- fun hideKeyboardFrom(context: Context, view: View) {
- val imm: InputMethodManager =
- context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
- imm.hideSoftInputFromWindow(view.windowToken, 0)
- }
- fun showSystemUI(): Int {
- return (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
- }
- fun hideSystemUI(): Int {
- return (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
- or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
- or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
- or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
- or View.SYSTEM_UI_FLAG_FULLSCREEN
- or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement