Search code examples
androidlayoutstylesscreen-sizesoftkeys

How to target devices with and without soft navigation keys (ie: Nexus 5, Galaxy S4)?


I was looking everywhere for an answer with no success. I am using the values-xxhdpi folder to set my app's layout dimensions for the nexus 5, but as I switch to the galaxy S4 emulator (same resolution but without soft navigation keys) the layout is not ok any more.

Is there a way to target devices having the same resolution with and without the soft navigation at the bottom of the screen?

Thanks!


Solution

  • use:

    ViewConfiguration.hasPermanentMenuKey()
    

    NOTE This only works for SDK >= 14

    EDITED:

    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    
    if (hasBackKey && hasHomeKey) {
        // no navigation bar, unless it is enabled in the settings
    } else {
        // 99% sure there's a navigation bar
    }