Advertisement
dzikovskyy

Get screen dimensions in pixels

Aug 31st, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. //If you want the display dimensions in pixels you can use getSize:
  2. Display display = getWindowManager().getDefaultDisplay();
  3. Point size = new Point();
  4. display.getSize(size);
  5. int width = size.x;
  6. int height = size.y;
  7.  
  8. //If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
  9.  
  10. WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  11. Display display = wm.getDefaultDisplay();
  12.  
  13. //Another ways is: DisplayMetrics
  14.  
  15. DisplayMetrics metrics = new DisplayMetrics();
  16. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  17.  
  18. //We can use widthPixels to get information for the absolute width of the display in pixels.
  19.  
  20. Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement