Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sticky immersive mode disabled after soft keyboard shown

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?

like image 458
jomalden Avatar asked Jun 12 '14 15:06

jomalden


People also ask

How do I turn off immersive mode on Android?

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).


2 Answers

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);     } 
like image 21
NOT_A_PROGRAMMER Avatar answered Sep 18 '22 21:09

NOT_A_PROGRAMMER


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. :)

like image 200
r3pwn Avatar answered Sep 16 '22 21:09

r3pwn