How to determine if an Android device has hard keys

How can I find out if an Android device has physical keys or a software navigation bar? I need to change the layout depending on whether the navigation system is drawn.

For example, HTC Desire C has hardware keys: enter image description here

I have to clarify - I'm looking at the navigation bar, not the keyboard. Home, back, etc. I tried:

getResources().getConfiguration().keyboard); getResources().getConfiguration().navigation); getResources().getConfiguration().navigationHidden); 

return the same values ​​on both devices.

+8
android
source share
3 answers

Having solved this, when you first launch the application and save the settings:

 public static boolean hasSoftKeys(WindowManager windowManager){ boolean hasSoftwareKeys = true; //c = context; use getContext(); in fragments, and in activities you can //directly access the windowManager(); if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){ Display d = c.getWindowManager().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; } 
+40
source share

Here you go .. this should do what you want.

 @SuppressLint("NewApi") private static boolean hasSoftNavigation(Context context) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH){ return !ViewConfiguration.get(context).hasPermanentMenuKey(); } return false; } 
+2
source share

private boolean isHardwareKeyboardAvailable() { return getResources().getConfiguration().keyboard != Configuration.KEYBOARD_NOKEYS; }

In addition, here is more information. http://developer.android.com/reference/android/content/res/Configuration.html#keyboard

EDIT - physical keyboard test

private boolean isPhysicalKeyboardAvailable() { return getResources().getConfiguration().keyboard == Configuration.KEYBOARD_QWERTY; }

For more information on viewing various configurations http://developer.android.com/reference/android/content/res/Configuration.html#keyboard

I am sure one of these works.

2nd EDIT -

Check Already asked.

Android: programmatically determine if a device has a hardware menu

0
source share

All Articles