Sharas
Sharas

Reputation: 2135

Detect soft navigation bar availability in onePlusOne device progmatically?

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.

Upvotes: 5

Views: 3547

Answers (5)

Anh Duy
Anh Duy

Reputation: 1183

//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
    }
}

Upvotes: 0

Atiq
Atiq

Reputation: 14835

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.

Upvotes: 0

Abdul Aziz
Abdul Aziz

Reputation: 472

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;
    }

Upvotes: 1

Amit Tiwari
Amit Tiwari

Reputation: 3692

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);
}

Upvotes: 2

Fred Grott
Fred Grott

Reputation: 3476

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

Upvotes: 0

Related Questions