3

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.

Hans1984
  • 796
  • 11
  • 24
Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69
  • Relevant post with answer is here: https://stackoverflow.com/questions/28983621/detect-soft-navigation-bar-availability-in-android-device-progmatically/32698387#32698387 – Raghu Mudem Dec 07 '21 at 10:26

1 Answers1

-1
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Akarsh M
  • 1,629
  • 2
  • 24
  • 47
  • This will give the size of window occupied by the application activity. But not the original size of the window. I have the same code in by question. Please verify the code added in the question. – Raghu Mudem Jul 15 '15 at 10:39