Updated 22 December 2016
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.
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(); |
1 2 3 |
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); float HeightInDp = displayMetrics.heightPixels / displayMetrics.density; float WidthInDp = displayMetrics.widthPixels / displayMetrics.density; |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.