Search code examples
javaandroidandroid-studioandroid-logcatsamsung-galaxy

Finding the actual available screen height on an s8+


I'm developing an application for android devices and want to support the samsung galaxy s8+ with an FHD+ resolution. However, when i log the emulator's height in the logcat, i get 2124x1080 (for FHD+) instead of *2220*x1080. Is that due to the navbar and the status bar? If that's the case how can i make sure that i am getting the right height in the logcat?

The code i am using for the width and height is simple:

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int height = dm.heightPixels;
    int width = dm.widthPixels;

Solution

  • First of all, I am slightly confused as to where you got those values from (2200 and 1080)

    According to Google's Device Metrics (https://material.io/devices/) The Samsung Galaxy S8+ has a size of 1440*2960)

    Besides that, it appears the code you have provided above returns the screen height, minus the navbar and status bar. If you want to find the height of these two UIElements, follow the code below.

    NavBar

    public int getNavBarHeight() { 
          int result = 0;
          int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
          if (resourceId > 0) {
              result = getResources().getDimensionPixelSize(resourceId);
          } 
          return result;
    }
    

    Status Bar

    public int getStatusBarHeight() { 
          int result = 0;
          int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
          if (resourceId > 0) {
              result = getResources().getDimensionPixelSize(resourceId);
          } 
          return result;
    }
    

    The above code returns the values in pixels, if you'd like to convert them to dp, use this method:

    public int pxToDp(int px) {
        DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
        return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    }