Small guide: how to support immersive mode under Android 4.4+

In Android 4.4 version we have new Immersive mode which allows to make system bars translucent and extend application area to fit all screen. This looks great as it gives more space to application in terms of usability and it creates more stylish look if application design is done accordingly.

Immersive mode for TripBudget

I added support of immersive mode to TripBudget. And there were some unexpected troubles on the way so i decided to write some kind of guide here.

Note: I talk here about immersive mode where bottom navigation bar is hidden – using SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag. This guide is aimed to gather and solve all problems which prevent sticky mode to work properly.

First we need to check if device supports immersive mode

There are several approaches how to enable the mode itself – i like programmatic way without any xml scheme modifications. We need just set some properties of window to enable translucent system bars (status bar and bottom navigation bar).

And than we setup View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY flag. This will hide ugly navigation bar on devices with no hardware buttons (like Nexus 4).

In perfect world that would be enough, but the main problem raises when we need to keep immersive mode during application life cycle. Solution suggested by Google is pretty simple – you just handle onSystemUiVisibilityChange / onWindowFocusChanged events. Like this:

But there are still some problems/bugs to solve:

  • Some versions of 4.4 breaks immersive mode when user pushes volume buttons
  • Sometimes onSystemUiVisibilityChange is not called at all!
  • When you switch between apps Immersive mode can be broken when is used with sticky mode (Lower navigation bar remains unhidden)
  • When you push back button on nav bar it becomes black and stays that way!

When we set mode for the first time we set some handlers:

Also we set the some refreshers at OnResume and OnWindowFocusChanged. And at onKeyDown event also!

As was proposed here for solution of volume up/down problem we handle onKeyDown event specific way. Not only we try to restore immersive mode instantly when event is fired but use delayed handler:

And the last part of tricky restore code – my function for restoring not just applies sticky mode but it clears it first. This solves problems when user switches between apps (checked on Nexus 4).

In my implementation handlers at resume and focus events call restoreTranslucentBarsDelayed();

Last concern – your app should be aware of transparent status bar. To get the height of status bar i use this code:

Enjoy using immersive mode 🙂

May be not all problems are yet covered in this small guide – so feel free to describe them and add your solutions in comments.