Check for navigation bar

Jonno picture Jonno · Apr 18, 2013 · Viewed 33.5k times · Source

I am trying to check to see whether the android navigation bar is present on load so that I can adjust a layout accordingly, does anyone have any suggestions?

This is the navigation bar I am trying to detect: enter image description here

P.S. All I have found so far are 'bad' ways to try and remove the bar, which I dont want to do.

Answer

philask picture philask · May 17, 2013

Took me some time but I've found a more reliable way than relying on hasPermanentMenuKey() which doesn't work for newer phones like the HTC One which have no menu key but do have home & back keys so don't need (or show) the soft navigation bar. To get around this try the following code which checks for a back button too:

boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

if(!hasMenuKey && !hasBackKey) {
    // Do whatever you need to do, this device has a navigation bar
}