layout - How to get screen dimensions as pixels in Android

ID : 482

viewed : 347

Tags : androidlayoutscreenpixeldimensionsandroid





Top 5 Answer for layout - How to get screen dimensions as pixels in Android

vote vote

92

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(); 

If you are in a fragment and want to acomplish this just use Activity.WindowManager (in Xamarin.Android) or getActivity().getWindowManager() (in java).

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 way 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); 

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); 

API level 30 update

final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();  // Gets all excluding insets  final WindowInsets windowInsets = metrics.getWindowInsets();  Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()          | WindowInsets.Type.displayCutout());   int insetsWidth = insets.right + insets.left;  int insetsHeight = insets.top + insets.bottom;   // Legacy size that Display#getSize reports  final Rect bounds = metrics.getBounds();  final Size legacySize = new Size(bounds.width() - insetsWidth,          bounds.height() - insetsHeight); 
vote vote

89

One way is:

Display display = getWindowManager().getDefaultDisplay();  int width = display.getWidth(); int height = display.getHeight(); 

It is deprecated, and you should try the following code instead. The first two lines of code gives you the DisplayMetrics objecs. This objects contains the fields like heightPixels, widthPixels.

DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics);        int height = metrics.heightPixels; int width = metrics.widthPixels; 

Api level 30 update

final WindowMetrics metrics = windowManager.getCurrentWindowMetrics();  // Gets all excluding insets  final WindowInsets windowInsets = metrics.getWindowInsets();  Insets insets = windowInsets.getInsetsIgnoreVisibility(WindowInsets.Type.navigationBars()          | WindowInsets.Type.displayCutout());   int insetsWidth = insets.right + insets.left;  int insetsHeight = insets.top + insets.bottom;   // Legacy size that Display#getSize reports  final Rect bounds = metrics.getBounds();  final Size legacySize = new Size(bounds.width() - insetsWidth,          bounds.height() - insetsHeight); 
vote vote

76

It may not answer your question, but it could be useful to know (I was looking for it myself when I came to this question) that if you need a View's dimension but your code is being executed when its layout has not been laid out yet (for example in onCreate() ) you can setup a ViewTreeObserver.OnGlobalLayoutListener with View.getViewTreeObserver().addOnGlobalLayoutListener() and put the relevant code that needs the view's dimension there. The listener's callback will be called when the layout will have been laid out.

vote vote

70

(2012 answer, may be out of date) If you want to support pre Honeycomb, you will need to put in backward compatibility prior to API 13. Something like:

int measuredWidth = 0; int measuredHeight = 0; WindowManager w = getWindowManager();  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {     Point size = new Point();     w.getDefaultDisplay().getSize(size);     measuredWidth = size.x;     measuredHeight = size.y; } else {     Display d = w.getDefaultDisplay();     measuredWidth = d.getWidth();     measuredHeight = d.getHeight(); } 

Of course the deprecated methods will eventually be taken out of the the most recent SDKs, but while we still rely on most of our users having Android 2.1, 2.2 and 2.3, this is what we are left with.

vote vote

51

I have tried all possible "solutions" unsuccessfully and I noticed that Elliott Hughes' "Dalvik Explorer" app always shows correct dimension on any Android device/OS version. I ended up looking at his open source project that can be found here: https://code.google.com/p/enh/

Here's all the relevant code:

WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; try {     // used when 17 > SDK_INT >= 14; includes window decorations (statusbar bar/menu bar)     widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);     heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } try {     // used when SDK_INT >= 17; includes window decorations (statusbar bar/menu bar)     Point realSize = new Point();     Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);     widthPixels = realSize.x;     heightPixels = realSize.y; } catch (Exception ignored) { } 

EDIT: slightly improved version (avoid firing exceptions on non-supported OS version):

WindowManager w = activity.getWindowManager(); Display d = w.getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); d.getMetrics(metrics); // since SDK_INT = 1; widthPixels = metrics.widthPixels; heightPixels = metrics.heightPixels; // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) try {     widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);     heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d); } catch (Exception ignored) { } // includes window decorations (statusbar bar/menu bar) if (Build.VERSION.SDK_INT >= 17) try {     Point realSize = new Point();     Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);     widthPixels = realSize.x;     heightPixels = realSize.y; } catch (Exception ignored) { } 

Top 3 video Explaining layout - How to get screen dimensions as pixels in Android







Related QUESTION?