Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I would like to check menu key presence in the device in android application. To achieve that i used following code to detect weather device is having hardware menu or not and it is working fine

!ViewConfiguration.get(ScsCommander.getInstance().getApplicationContext()).hasPermanentMenuKey()

But i did not find a logic to find weather the device is having soft menu present or not.

Please suggest me is there any way to detect soft menu is available or not in the device.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
334 views
Welcome To Ask or Share your Answers For Others

1 Answer

There is no reliable/clean way to check if soft menu (aka Navigation bar) is present or not!

You may try using below code (not tested on all devices and not a reliable solution anyways):

boolean hasNavBar(Context context) {
    Resources resources = context.getResources();
    int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) {
        return resources.getBoolean(id);
    } else {    // Check for keys
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }
}

Here, we are fetching the resource identifier using Resources class. We are looking for a resource "navigation bar" which is passed as the 1st parameter.

The getIdentifier returns the associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

In case if this approach fails, in else we are trying to see check if certain physical keys are present on the device like back or Home which usually constitute Navigation bar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...