0

Searching on google i found various way get the screen size, here's what i got.

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    Log.i(TAG, "first way width = "+size.x);
    Log.i(TAG, "first way height = "+size.y);

    int width = getWindowManager().getDefaultDisplay().getWidth();
    int height = getWindowManager().getDefaultDisplay().getHeight();
    Log.i(TAG, "second way width = "+width);
    Log.i(TAG, "second way height = "+height);

    int height2 = Resources.getSystem().getDisplayMetrics().heightPixels;
    int width2 = Resources.getSystem().getDisplayMetrics().widthPixels;
    Log.i(TAG, "third way width = "+width2);
    Log.i(TAG, "third way height = "+height2);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height3 = displaymetrics.heightPixels;
    int width3 = displaymetrics.widthPixels;
    Log.i(TAG, "fourth way width = "+width3);
    Log.i(TAG, "fourth way height = "+height3);  

On logging all are giving the same result. I was wondering if there any difference between all these methods or any specific use cases? Thanks in advance.

Rahul Das
  • 31
  • 1
  • 4

2 Answers2

1

Another way is this :

public static DisplayMetrics displayMetrics;

displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

for the width of the screen :

float screenWidth = displayMetrics.widthPixels;

for the height of the screen :

float screenHeight = displayMetrics.heightPixels;
Ahmad Sabeh
  • 526
  • 5
  • 18
0

Don't reinvent the wheel use it, this is the code to check screen height and width:

/**
     * Return the width of screen, in pixel.
     *
     * @return the width of screen, in pixel
     */
    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return Utils.getApp().getResources().getDisplayMetrics().widthPixels;
        }
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    /**
     * Return the height of screen, in pixel.
     *
     * @return the height of screen, in pixel
     */
    public static int getScreenHeight() {
        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return Utils.getApp().getResources().getDisplayMetrics().heightPixels;
        }
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }

picked from this helper library by BlankJ

Zulqurnain Jutt
  • 1,077
  • 1
  • 9
  • 21