This code will return the width (w) and height (h) of the screen.
DisplayMetrics dMetrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics); float density = dMetrics.density; int w = Math.round(dMetrics.widthPixels / density); int h = Math.round(dMetrics.heightPixels / density);
activity is an instance of Activity for which you want to get the screen size.
You must remember that: when your device is in landscape orientation, w> h. When he is in portrait orientation w <h.
So, in width and height you can find that your device is in what orientation.
source share