How to get the screen size of the device?
Asked Answered
V

4

8

The desire have 480 x 800 pixels, 3.7 inches and the HD have 480 x 800 pixels, 4.3 inches screen specification.

I run the code that is accepted as answer from this thread How to get screen size of device? but it is wrong. For example for screen size of 3.7 inches returns 2.4 which is very wrong.

Does anybody know how to get the real screen size? I do not need the resolution I need the screen size of the screen in inches.

NOTE: this code is wrong (at lest doesn't work for me)

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //give me 320
int height = dm.heightPixels; //give me 533

I have HTC Desire and my resolution is 480 x 800 pixels,

Can someone please tell me how to get the real screen size in inches, and get the real resolution of the device?

Visitor answered 27/7, 2011 at 7:33 Comment(3)
android.util.DisplayMetrics is the structure you need to use. this question and the answer , is all you need. #3167001Nitrite
I doesn't work for me, please see my editsVisitor
see my answer here.. #15055958Phenformin
V
16

The code works fine but it doesn't return pixels, it returns DIP (Density independent pixels ) which is not the same.

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320 dip
int height = dm.heightPixels; //533 dip

What I needed is the REAL pixels and the calculation for them is like this:

int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));

now the widthPix is 480

Visitor answered 27/7, 2011 at 8:16 Comment(1)
just an addition that the dm.densityDpi/160.0 can also be found by dm.density or may be dm.scaledDensityPate
D
8

This is exactly what happens if you explicitly disable pre-scaling. See the second bullet point of this document (a quick Google search on the term "320x533" turned up lots of other people who've encountered this). Get rid of your pre-scaling and the discrepancy should go away (getWidth()/getHeight() would then return the actual size, not the scaled size.

Note for tablet devs: getWidth()/getHeight() will not return the raw screen size because the bottom bar (48 pixels or so) isn't part of the area an app can draw to. You'll get the size of everything you can draw to (e.g., 1280x752 rather than the full 1280x800 in landscape mode). Again, no pre-scaling means these values are accurate and exact.

Dobbs answered 21/10, 2011 at 13:55 Comment(0)
A
0

Disable pre-scaling by adding this line to your manifest file:

<supports-screens android:anyDensity="true" />

Position:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="...."
  android:versionCode="1"
  android:versionName="1.0">

<supports-screens android:anyDensity="true" />

<application android:label="@string/app_name" >
...
Accommodation answered 21/12, 2013 at 9:51 Comment(2)
Actually you should set it to "false" instead of "true" for disabling. The default value is already "true" for "anyDensity" attribute. src:developer.android.com/guide/topics/manifest/… ps:editted your answer accordingly.Gammer
NOTE: Android docs say "... you should not set it "false" unless you're absolutely certain that it's necessary for your application to work." developer.android.com/guide/topics/manifest/… - so it might be useful for a test, but dubious to ship your app that way, unless you know all the ramifications of doing so.Lola
P
-1

Try using Display class rather than DisplayMetrics.

Display dispOb = getWindowManager().getDefaultDisplay();
String widthAndHeight= dispOb.getWidth()+"X"+dispOb.getHeight();
Pate answered 14/7, 2012 at 11:6 Comment(1)
This is not an answer. Does this work or not? I can try hundreds of things, but I am here to find an answer.Dairen

© 2022 - 2024 — McMap. All rights reserved.