In this small blog,
We will show you the simple methods to get your screen width and height in pixels as well as in dp.
Screen width or height in pixels
If you have used this method in an Activity,
1 2 3 4 5 |
Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; |
If you’re not in an Activity you can get the default Display via WINDOW_SERVICE:
1 2 |
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); |
Screen width or height in dp
1 2 3 |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float HeightInDp = displayMetrics.heightPixels / displayMetrics.density; float WidthInDp = displayMetrics.widthPixels / displayMetrics.density; |