5

I need to check if a device has the soft navigation bar, and I followed the suggestions here.

It works great, except on onePlus devices, for some reason, this code:

int id = resources.getIdentifier("config_showNavigationBar", "bool", android");
    return id > 0 && resources.getBoolean(id);

returns false, although the soft navigation bar is displayed.

Any idea how can I get the correct result?

I prefer not to calculate the real width and available width, it seems like expensive operation.

Thanks.

Community
  • 1
  • 1
Sharas
  • 1,985
  • 3
  • 20
  • 43
  • did you get a solution for this ? – Suresh Kumar Oct 12 '16 at 14:26
  • 1
    Yes. Eventually, I used this code: public boolean hasNavBar() { Display d = getWindowManager().getDefaultDisplay(); DisplayMetrics dm = new DisplayMetrics(); d.getRealMetrics(dm); int realHeight = dm.heightPixels; int realWidth = dm.widthPixels; d.getMetrics(dm); int displayHeight = dm.heightPixels; int displayWidth = dm.widthPixels; return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0; } This gave the best result on all devices. – Sharas Dec 13 '16 at 09:58

5 Answers5

2

See this answer. There is no way be 100% sure, though.

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
}

Edit

Another approach

public boolean hasNavBar (Resources resources) {
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    return id > 0 && resources.getBoolean(id);
}
Community
  • 1
  • 1
Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
1

Yes you can try this:

    WindowManager mgr = (WindowManager) getSystemService(WINDOW_SERVICE);
            boolean hasSoftKey = Utils.hasSoftKeys(mgr, NPTApplication.this);

public static boolean hasSoftKeys(WindowManager windowManager, Context c) {
        boolean hasSoftwareKeys = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Display d = windowManager.getDefaultDisplay();

            DisplayMetrics realDisplayMetrics = new DisplayMetrics();
            d.getRealMetrics(realDisplayMetrics);

            int realHeight = realDisplayMetrics.heightPixels;
            int realWidth = realDisplayMetrics.widthPixels;

            DisplayMetrics displayMetrics = new DisplayMetrics();
            d.getMetrics(displayMetrics);

            int displayHeight = displayMetrics.heightPixels;
            int displayWidth = displayMetrics.widthPixels;

            hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
        } else {
            boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
            boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
            hasSoftwareKeys = !hasMenuKey && !hasBackKey;
        }
        return hasSoftwareKeys;
    }
Abdul Aziz
  • 442
  • 5
  • 12
0

no will not work that way you have to compute the size

the method used is detailed in this SO answer;

How to get height and width of navigation bar programmatically

Community
  • 1
  • 1
Fred Grott
  • 3,505
  • 1
  • 23
  • 18
0

Well, there is a method hasPermanentMenuKey which checks if the hardware MenuKey is present, usually Samsung devices has it on left size of Home button.

So if it returns true, it means the phone has hardware keys, and if it's false, then simply it means phone has navigation bar.

The method is:

ViewConfiguration.hasPermanentMenuKey()

I find it pretty useful for myself.

halfer
  • 19,824
  • 17
  • 99
  • 186
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

//The method has ability to return 0 when the navigation bar was hidden.

private fun getNavigationBarHeight(context: Context): Int {
    val display = context.windowManager?.defaultDisplay
    return if (display == null) {
        0
    } else {
        val realMetrics = DisplayMetrics()
        display.getRealMetrics(realMetrics)
        val metrics = DisplayMetrics()
        display.getMetrics(metrics)
        realMetrics.heightPixels - metrics.heightPixels
    }
}
Anh Duy
  • 1,145
  • 15
  • 25