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.
Related
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);
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!
so I have written the following method in my activity:
private void setDisplayMetrics(){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int dh = metrics.heightPixels;
int dw = metrics.widthPixels;
if(dw < dh){
deviceWidth = dw;
deviceHeight = dh;
}else{
deviceWidth = dh;
deviceHeight = dw;
}
System.err.println("--------------> dh : "+deviceHeight+" | dw "+deviceWidth);
}
And it works great, in the sense that it gets me the total width and height of the screen with great accuracy and reliability (which is what I have asked it to do).
Here is the problem. On older android devices the screen dimensions are the same as the dimensions the application can take up, and the script above helps me to set the size of elements in the app. BUT with android ICS I have this graphic button bar on the bottom of the screen, and it messes up my whole strategy.
What I would really like is the ability to get the available app dimensions for the portrait view as well as the landscape view at the same time in one method. And have these dimensions be accurate regardless of the presence of the bar pictured above.
Does anyone know how to achieve this?
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
From what I've read so far you can't get this height directly. The approach via getWindowVisibleDisplayFrame() does not seem to work.
The only promising solution I have found so far is using a custom layout to measure the screen size as explained in
http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html.
I am convinced that this will work but to me it seems to be more trouble than it is worth.