6 Apr 2016

Get screen dimensions in pixels

If you want the display dimensions in pixels you can use getSize :
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:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated

For the use case you're describing however, a margin/padding in the layout seems more appropriate.
Another ways is: DisplayMetrics.
A structure describing general information about a display, such as its size, density, and font scaling. To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

We can use widthPixels to get information for:
"The absolute width of the display in pixels."
Example:

Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

2 comments:

  1. Hi, i think that i saw you visited my site so i came to “return the favor”.I am trying to find things to improve my site!I suppose its ok to use some of your ideas!!apple kundenservice berlin

    ReplyDelete
  2. Tech Accents This website contains thousands of apps that can be downloaded from their website. The site also provides APK versions of apps that are not available on the Google Play Store. So lets see

    ReplyDelete