Check navigation bar

I am trying to check if the Android navigation bar is available for download so that I can set the layout correctly, does anyone have any suggestions?

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

PS All I have found so far are the "bad" ways to try and remove the panel, which I do not want to do.

+44
java android layout android-4.2-jelly-bean
Apr 18 '13 at 20:27
source share
10 answers

The time has come, but I found a more reliable way than relying on hasPermanentMenuKey () , which does not work for newer phones such as HTC One , which have no menu keys, but you have home and back keys, so no need (or show) soft navigation bar. To get around this, try the following code, which also checks the return button:

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 } 
+43
May 17 '13 at 12:00
source share

There is no reliable way to check the navigation bar. Using KeyCharacterMap.deviceHasKey , you can check if certain physical keys are available on the device, but this information is not very useful, since devices with physical keys can still have a navigation bar. Devices, such as OnePlus One or any device that works with custom rum, have an option in the settings, which disable the physical keys, and adds a navigation bar. There is no way to check if this option is enabled, and deviceHasKey still returns true for keys that are disabled by this option.

This is the closest you can get:

 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 a navigation bar } 

If the "Back" and "Home" buttons are not physically present on the device, it should have a navigation bar because the user will not be able to move at all. However, you can never be 100% sure of this, as manufacturers may not implement deviceHasKey correctly.

+30
Apr 20 '14 at 10:49
source share

Here is a quick answer that combines the solutions of Pauland and Philask. I’m afraid that I don’t have enough devices to test if they work everywhere. I would be interested to hear other results.

 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; } } 
+8
Mar 18 '15 at 11:01
source share

Another solution (part of my UtilsUISystem class)

  public static boolean hasNavBar (Resources resources) { //Emulator if (Build.FINGERPRINT.startsWith("generic")) return true; int id = resources.getIdentifier("config_showNavigationBar", "bool", "android"); return id > 0 && resources.getBoolean(id); } 
+7
Jan 30 '15 at 11:38
source share

you can add this code to your activity in the onCreate () method:

  View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener (new View.OnSystemUiVisibilityChangeListener() { @Override public void onSystemUiVisibilityChange(int visibility) { if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) { // TODO: The navigation bar is visible. Make any desired // adjustments to your UI, such as showing the action bar or // other navigational controls. } else { // TODO: The navigation bar is NOT visible. Make any desired // adjustments to your UI, such as hiding the action bar or // other navigational controls. } } }); 
+1
Aug 11 '16 at 8:43
source share

This method worked for me

  int id = getResources().getIdentifier("config_showNavigationBar","bool","android"); boolean result = id > 0 && getResources().getBoolean(id); // if(result) { // Do whatever you need to do, this device has a soft Navigation Bar } 

It worked for me and was tested on many devices.

+1
May 24 '17 at 20:49
source share

Something that should work better is to measure the screen.

Starting with API 17 there getWindowManager().getDefaultDisplay().getRealSize() , which can be compared with the size returned by getWindowManager().getDefaultDisplay().getSize() .

If you have different results, I consider it safe to say that there is a navigation bar, and if you get the same results, it is not. One thing worth paying attention to is your target SDK and supported screens, which can lead to scaling of the result of getSize() if Android believes that your application will not work on the current device without scaling.

Below API 17, you can measure the screen using getWindowManager().getDefaultDisplay().getMetrics() both landscape and portrait modes, and again, different results probably mean that there is a navigation bar.

However, if you get the same results, you don’t really know, since phones can hold the navigation bar at a shorter edge even in the landscape. It is clear that if the width or height is 4-8% less than standard sizes, such as 1280x800 , 1280x720 , 1024x600 , while the other dimension is equal, then again, there is probably a navigation bar. Do not bet on this. Too many permissions that differ too little from each other for this to work well.

0
May 21 '15 at 7:33
source share
 boolean hasNavBar(Context context) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { // navigation bar was introduced in Android 4.0 (API level 14) 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; } } else { return false; } } 
0
Feb 16 '16 at 7:50
source share

I see the answers above, I want to indicate that "does not exist" can be considered as a height of 0; so it could be like this:

 public static int getScreenH(Context context) { DisplayMetrics dm = new DisplayMetrics(); dm = context.getResources().getDisplayMetrics(); int h = dm.heightPixels; return h; } public static int getDpi(Context context) { DisplayMetrics displayMetrics1 = context.getResources().getDisplayMetrics(); int height1 = displayMetrics1.heightPixels; int dpi = 0; WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = windowManager.getDefaultDisplay(); DisplayMetrics displayMetrics = new DisplayMetrics(); @SuppressWarnings("rawtypes") Class c; try { c = Class.forName("android.view.Display"); @SuppressWarnings("unchecked") Method method = c.getMethod("getRealMetrics", DisplayMetrics.class); method.invoke(display, displayMetrics); dpi = displayMetrics.heightPixels; } catch (Exception e) { e.printStackTrace(); } return dpi; } public static int getBottomStatusHeight(Context context) { int totalHeight = getDpi(context); int contentHeight = getScreenH(context); return totalHeight - contentHeight; } 

`` ``

0
Nov 15 '16 at 3:42 on
source share

Decision. Only devices without constant hardware keys have a navigation bar, so you can check the API version and use hasPermanentMenuKey () to search for hardware keys

  boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey(); 
-2
Apr 19 '13 at 15:17
source share



All Articles