How to get screen DPI - android

I'm trying to get the screen's dpi but so far it isn't working. I tried:
DisplayMetrics dm = new DisplayMetrics();
mainContext.getWindowManager().getDefaultDisplay().getMetrics(dm);
SCREEN_DPI = dm.densityDpi;
But both on Samsung Galaxy Tab 10.1 and on Samsung Galaxy S I9000 the SCREEN_DPI is equal to 160. I also tried SCREEN_DPI = dm.density, but I get the value 1.0 to both cases.
Any help would be greatly appreciated.

Duplicate of this question
Though Android doesn't use a direct pixel mapping, it uses a handful
of quantized Density Independent Pixel values then scales that to the
actual screen size. So the density property will be one of those
constants (120, 160, or 240 dpi).
If you need the actual density (perhaps for an OpenGL app) you can get
it from the xdpi and ydpi properties for horizontal and vertical
density respectively.

Try getRealMetrics() instead:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
This API was officially added in API 17, but in my limited testing it's worked correctly even on 4.0 devices, so it may have been a hidden API before that.
Official documentation here, though there seems to be more reasons that the other API doesn't do what you expect than the text there would imply.

According to #CommonsWare: "Samsung is welcome to categorize them (the devices) to be in whatever density bucket they wish". So, in this case, both devices are in the same density bucket.

Related

values-sw resource issue in Android

I have two devices:
Moto-G first generation (720*1280, 4.5" 324.2 PPI)
ICE Tablet (600*1024 7" 169.55 PPI)
And created folders "values-sw420dp-xhdpi" and "values-xhdpi" for Moto-G and folder "values-sw360dp-mdpi" for ICE Tablet. But both devices taking the values from "values-sw360dp-mdpi" folder.
I am very confuse about this behavior of the devices because both are quite different, however why this is happening ?
Can anybody please guide me how to define sw folder for these two devices and in general how sw is working? I read the official guide line of sw folder and supporting multiple screens, but did not got exact idea.
According to your question, let me cite a fragment from Android Developers Guide:
Screen configuration: smallestWidth
Qualifier values: swdp
Examples: sw600dp sw720dp
The fundamental size of a screen, as indicated by the shortest
dimension of the available screen area. Specifically, the device's
smallestWidth is the shortest of the screen's available height and
width (you may also think of it as the "smallest possible width" for
the screen). You can use this qualifier to ensure that, regardless of
the screen's current orientation, your application's has at least <N>
dps of width available for its UI.
For example, if your layout requires that its smallest dimension of
screen area be at least 600 dp at all times, then you can use this
qualifier to create the layout resources, res/layout-sw600dp/. The
system will use these resources only when the smallest dimension of
available screen is at least 600dp, regardless of whether the 600dp
side is the user-perceived height or width. The smallestWidth is a
fixed screen size characteristic of the device; the device's
smallestWidth does not change when the screen's orientation
changes.
Full Text: Supporting Multiple Screens
Like #tiny said:
sw420dp-xhdpi means screenWidth = 840(420 *2) ,
sw360dp-mdpi means screenWidth = 360(360*1)
EDIT: Using this site http://pixeldensitycalculator.com/ I've already calcualated that
Moto G 1st edition has density 293.72 ~ 320dpi
ICE Tablet has density 169.55 ~ 160dpi
None of these devices doesn't have more than 320dpi, that's why none of them using your 420dpi folder.
Hope it help

Want fixed size image or drawable ( in inches ) on all devices

I tried this doing with device dpi. Getting dpi and scaling image accordingly but does not work on all devices. I have written code in cocos2dx framework and tried it on android, devices like xolo and xiomi my note does not display the desired result (image size different from rest devices).
My concept is if one device has 320 dpi and another has 240 dpi than
1 inch = 320 px(in 1st device) = 240 px(in 2nd device)
for android I have used
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.xdpi
metrics.ydpi
and scaled accordingly for each phone
Is it possible that all devices not return correct dpi.
Is this a correct way of doing it?
Is there an alternate solution for obtaining this result?
For some devices xdpi and ydpi metrics are returning wrong numbers and you cannot rely on them. Since API 17 you can also use getRealMetrics(). It should give you accurate values for xdpi and ydpi.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(dm);
dm.xdpi
dm.ydpi

Android DisplayMetrics inconsistent between devices

I'm trying to use DPI and touch input in my unity game to recognise gestures consistently across android devices of different sizes.
For instance, I want my virtual thumbstick to be two inches from "full left" to "full right". Given the DPI of the screen, I can convert touches from pixels to inches like this: inchesFromStickCenter = pixelsFromStickCenter/dpi.
To get DPI, unity offers Screen.dpi but I was getting inconsistent results across devices. Sometimes the thumbstick was way too big, sometimes way too small. Instead I went straight to Android's DisplayMetrics, where I could get xdpi, ydpi, and densityDpi. From this question Difference between the declared size in inches and the size obtained using DisplayMetrics I see that densityDpi is a rounded value, and I should probably be using xdpi or ydpi.
I tested things by trying to compute the width of the screen in inches (my is landscape, all these examples assume landscape). When I divide the screen pixel width (1280) by xdpi (195.4) on my 1st gen N7, it overestimates the screen width by half an inch (6.55 inches, compared to just under 6 when measured with a rule). When I divide by densityDpi (213), it's a much better answer. The wikipedia page for the N7 says dpi is 215, which would also give a great answer.
When I test on my Galaxy S2, the xdpi (217) gives a good screen size estimate, and the densityDpi (240) gives an underestimate by a third of an inch.
So I can't depend on either of these numbers! Why is neither N7 stat nothing like the wikipedia page? Is this a silly way of trying to convert pixels to real-world inches? What should I be doing instead?
Cheers!

Can I change device DPI programmatically?

I was wondering if I can change device which decides which layout it should choose or which graphic it should choose/scale. This change should only affect my application of course. Can I set somehow device DPI?
The reason why I'm doing that is this phone: http://www.gsmarena.com/samsung_p1000_galaxy_tab-3370.php it has very bad DPI od 240DPI while it's only 170PPI and should be recognized as 160DPI device.
I have quite big graphic for layout-large which looks great on mdpi, but on hdpi they doesn't fit, they are just to big.
Maybe you have another solution than changing device DPI?
Yes it is possible.
The following will set the dpi to 240. Although you have to refresh the activity somehow e.g. restart application.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.density = 1.5f;
metrics.densityDpi = 240;
metrics.heightPixels = 1104;
metrics.widthPixels = 1920;
metrics.scaledDensity = 1.5f;
metrics.xdpi = 254.0f;
metrics.ydpi = 254.0f;
getResources().getDisplayMetrics().setTo(metrics);
you should create your application
to support with multiple screens.take a look at documentation
and hope these links helps you
Designing for Multiple Screens
Working with Multiple Android Screens
Samsung Galaxy Tab GT-P1000 take layout from layout-xhdpi if this not there then it will look for large.
Try to support multiples screen resolution for your app based on that documentation
you have to Design alternative layouts and drawables for your app

List of densities for different android devices

Is there a list of density values for different android devices? I know how to check it programmatically but I this doesn't work if I don't have a specific device.
As a general rule, if you know the screen resolution of the device you can determine what density bucket it's going to fall into. This is because density is a function of resolution and physical size, and manufacturers tend to pick a screen size that fits the display resolution. As such, here is a list I use of all the common resolutions found on the market.
LDPI: QVGA (320x240), WQVGA (432x240)
MDPI: HVGA (480x320), WXGA (1280x800), SVGA (1024x600)
HDPI: WVGA (800x480), FWVGA (854x480)
The reason those two really high resolutions exist in MDPI is because they are only used on tablets, which have a larger screen size...so the dpi rating works out to still be in the midrange.
Also, you may find this information on Screen Sizes and Densities useful, from the Developer portal.
HTH!
use this code for it frnd: scale gives you density:
for low scale:0.75
for medium scale:1.0
for large scale:1.5
float scale;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
scale =getApplicationContext().getResources().getDisplayMetrics().density;

Categories

Resources