In my android application I need to find out if the device is having a navigation bar or not. For that I am getting the device original screen size and application window size. Based on that I am calculating the difference and so I can find out if the device is having a navigation bar or not. Here is the code I use:
public static boolean hasSoftKeys(WindowManager windowManager){
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;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
}
The problem is: Calling the "getRealMetrics
" method requires api level 17. Here I need a solution for the lower version devices which will give the same result like getRealMetrics
to get the original screen size. I did not find any solution.
Can anybody suggest me any alternative for getRealMetrics
which will work for the lower version devices ?
Here is my investigation to find out the navigation bar availability. It is not a reliable result on all devices.
Code1:
boolean hasNavBar(Context context) {
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !hasMenuKey && !hasBackKey;
}
Code2
boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
By using this code we can check if the device has PermanentMenuKey. But it is not the meaning device which don't have PermanentMenuKey is having soft navigation bar.