I have an app that needs to be full screen most of the time. I know that if an alert is shown or other window is displayed, over the top of the activity window, full screen is temporarily removed. Unfortunately, when a soft keyboard is shown for an EditText or something, when the user has finished with the keyboard, full screen immersive mode is not restored.
Any idea how this can be achieved?
Non-sticky (normal) immersive mode — A user can exit immersive mode, by swiping in the system bars. Sticky immersive mode — A user can temporarily exit immersive mode by swiping in the system bars. Immersive mode is automatically re-entered after a short time (few seconds).
I put this code at onCreate() observer the layout changes
getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int screenHeight = getWindow().getDecorView().getRootView().getHeight(); int keyboardHeight = screenHeight - rect.bottom; if (keyboardHeight > screenHeight * 0.15) { setToImmersiveMode(); } } }); private void setToImmersiveMode() { // set to immersive getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); }
Taken from this sample app by Google, you need to append this to the end of your activity, before the last end bracket:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); // When the window loses focus (e.g. the action overflow is shown), // cancel any pending hide action. When the window gains focus, // hide the system UI. if (hasFocus) { delayedHide(300); } else { mHideHandler.removeMessages(0); } } private void hideSystemUI() { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LOW_PROFILE | View.SYSTEM_UI_FLAG_IMMERSIVE ); } private void showSystemUI() { getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN ); } private final Handler mHideHandler = new Handler() { @Override public void handleMessage(Message msg) { hideSystemUI(); } }; private void delayedHide(int delayMillis) { mHideHandler.removeMessages(0); mHideHandler.sendEmptyMessageDelayed(0, delayMillis); }
And you should be good. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With