In debug mode,
metrics.xdpi and .ydpi both return 160. However, I have a 5" 1920x1080 phone. What am I doing wrong?
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
double x = Math.pow(size.x/metrics.xdpi,2);
double y = Math.pow(size.y/metrics.ydpi,2);
EDIT: I am trying to get pixel density to calculate physical screen size in inches. My phone has 400+ pixel density, but xdpi and ydpi only return 160.
I played around with DisplayMetrics and I found that metrics.densityDpi gives me much more accurate DPIs than x and ydpi. Thanks!
Related
I prepared for my two devices two different layouts. First I checked the width and height with this code:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
It showed me that, first device has 360dp width and 592dp height and the second 360dp width and 692dp height. So I prepared two layouts in such folders layout-w360dp-h592dp and layout-w360dp-h692dp.
And it turns out the devices don't use the layout that they are dedicated to. 360dp width and 592dp height device uses default layout and 360dp width and 692dp height device use layout for 360dp width and 592dp height.
Where is the problem? Thank you in advance!
I'm making a game for Android using Java, I need to map the game screen dimensions to the device screen dimensions, I used to use the following code to get the device screen dimensions
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
I face a problem with new devices with a notch, the above code gives me different screen height than the actual device, for example, the actual device height is 2340 pixels but the code returned 2130 pixels, I used the following code to get the notch height
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
DisplayCutout displayCutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
if (displayCutout.getBoundingRects().size() > 0) {
Rect notchRect = displayCutout.getBoundingRects().get(0);
}
}
which is 80, now 2130 + 80 is 2210, there are still 130 pixels missing when I map the game screen to the device screen, a shift happens due to the incorrect height
Until now there is no way to get the real dimensions of the screen with a notch
Have you tried getRealMetrics instead of getMetrics?
I'm trying to get the pixel width/height of the device for placing elements and mapping touch events on the screen but not getting the desired effect (notice lower right square should be completely flush with the bottom right):
// screen size
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float ScrDensity = metrics.density;
int metricHeight = metrics.heightPixels;//
int metricWidth = metrics.widthPixels;//
Log.i("AllMetrics", metrics.toString());
// temp
int rightButton_diameter = 100;
int rightButton_xpos = metricWidth - rightButton_diameter;
int rightButton_ypos = metricHeight - rightButton_diameter;
Where AllMetrics returns:
DisplayMetrics{density=2.0, width=720, height=1184, scaledDensity=2.0, xdpi=320.0, ydpi=320.0}
I notice that the returned height is also different from the 720x1280 listed in the Virtual Device Manager (API 19, Android 4.4) and the x/y positions of the touch events seems accurate to the drawn elements (ie the clickable area of the button is the same as where it is being drawn)
Is this a configuration error or am I using the incorrect method of fetching screen position?
Check this code it works for me
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Outside of activity you can try this
((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm)
To access the DisplayMetrics members, initialize an object like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
I want to get the actual screen height of the device that runs my app. To achieve this i try the following:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);
int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);
The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?
see this answer
if your API level > 13 try this if you are in activity
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();
getWidth and getHeight is for API level 13 or less
UPDATE
For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:
Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).
int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;
I needed to get the actual available height, and out of all the options this is the only thing that worked for me.
Create a rectangle, and get its dimensions:
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;
where activityRootView is the root view of your layout.
If you need to extract the toolbar height:
int availableScreenHeight = screenHeight - toolbar.getHeight();
where toolbar is your toolbar view.
availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).
you can use this code:
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
You can try this:
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Point size = new Point();
d.getSize(size);
Log.i("LOG", "W=" + size.x + " , H=" + size.y);
i have used
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
cube.height=metrics.heightPixels;
cube.width=metrics.widthPixels;
to get height and width of my display but the resolution is low for my sony xperia p device which is qhd display. from this function i can only get height=569 px and width=320 px
i don't understand why this is showing lower. i think this is giving me virtual resolution than real one. i used simple view and ondraw method to draw the canvas.
please help.
Maybe you should try something like this:
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawWidth = (Integer) mGetRawW.invoke(display);
int rawHeight = (Integer) mGetRawH.invoke(display);
As quoted from a website on this question.