How do I get the height and width of the Android Navigation Bar programmatically? How do I get the height and width of the Android Navigation Bar programmatically? android android

How do I get the height and width of the Android Navigation Bar programmatically?


Try below code:

Resources resources = context.getResources();int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");if (resourceId > 0) {    return resources.getDimensionPixelSize(resourceId);}return 0;


I get navigation bar size by comparing app-usable screen size with real screen size. I assume that navigation bar is present when app-usable screen size is smaller than real screen size. Then I calculate navigation bar size. This method works with API 14 and up.

public static Point getNavigationBarSize(Context context) {    Point appUsableSize = getAppUsableScreenSize(context);    Point realScreenSize = getRealScreenSize(context);    // navigation bar on the side    if (appUsableSize.x < realScreenSize.x) {        return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);    }    // navigation bar at the bottom    if (appUsableSize.y < realScreenSize.y) {        return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);    }    // navigation bar is not present    return new Point();}public static Point getAppUsableScreenSize(Context context) {    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    Display display = windowManager.getDefaultDisplay();    Point size = new Point();    display.getSize(size);    return size;}public static Point getRealScreenSize(Context context) {    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    Display display = windowManager.getDefaultDisplay();    Point size = new Point();    if (Build.VERSION.SDK_INT >= 17) {        display.getRealSize(size);    } else if (Build.VERSION.SDK_INT >= 14) {        try {            size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);            size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);        } catch (IllegalAccessException e) {} catch (InvocationTargetException e) {} catch (NoSuchMethodException e) {}    }    return size;}

UPDATE

For a solution that takes into account display cutouts please check John's answer.


The NavigationBar height varies for some devices, but as well for some orientations. First you have to check if the device has a navbar, then if the device is a tablet or a not-tablet (phone) and finally you have to look at the orientation of the device in order to get the correct height.

public int getNavBarHeight(Context c) {         int result = 0;         boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();         boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);         if(!hasMenuKey && !hasBackKey) {             //The device has a navigation bar             Resources resources = c.getResources();             int orientation = resources.getConfiguration().orientation;             int resourceId;             if (isTablet(c)){                 resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");             }  else {                 resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_width", "dimen", "android");                  }             if (resourceId > 0) {                 return resources.getDimensionPixelSize(resourceId);             }         }         return result;} private boolean isTablet(Context c) {    return (c.getResources().getConfiguration().screenLayout            & Configuration.SCREENLAYOUT_SIZE_MASK)            >= Configuration.SCREENLAYOUT_SIZE_LARGE;}