26 Jun 2015

How to get the screen density programmatically in android?

How to find the screen dpi of the current device?

You can get info on the display from the DisplayMetrics struct:
DisplayMetrics metrics = getResources().getDisplayMetrics();
getResources().getDisplayMetrics().density;
Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size. So the metrics.densityDpi property will be one of the DENSITY_??? constants (120, 160, 213, 240, 320, 480 or 640 dpi).
If you need the actual lcd pixel density (perhaps for an OpenGL app) you can get it from the metrics.xdpi and metrics.ydpi properties for horizontal and vertical density respectively.
If you are targeting API Levels earlier than 4. The metrics.density property is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpican be calculated
int densityDpi = (int)(metrics.density * 160f);
Here,

0.75 - ldpi
1.0 - mdpi
1.5 - hdpi
2.0 - xhdpi
3.0 - xxhdpi
4.0 - xxxhdpi


Or,
Use follwing code,

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                 break;
     case DisplayMetrics.DENSITY_HIGH:
                 break;
}
This will work in API lavel 4 or higher.

More details about px, dip, dp and sp units in Android

1) px = dp * ( dpi / 160 )
2) px is one pixel.
3) sp is scale-independent pixels.
4) sp for font sizes
5) dip is Density-independent pixels.
6) dip for everything else, where dip == dp
7) 100*100 px image for mdpi
8) 150*150 px image for hdpi
9) 200*200 px image for xhdpi
10) 1080x1920    save it in “drawable-xxhdpi” folder
11) 480x800      save it in “drawable-hdpi” folder
12) 320x480      save it in “drawable-mdpi” folder
13) 1280x720     save it in “drawable-xhdpi” folder
14) If running on mdpi device, 150x150 px image will take up 150*150 dp of screen space.
15) If running on hdpi device, 150x150 px image will take up 100*100 dp of screen space.
16) If running on xhdpi device, 150x150 px image will take up 75*75 dp of screen space.

Supporting Multiple Screens, Sizes and Densities

Use dp (== dip) for everything but for fonts use sp
Pretty much everything about this and how to achieve the best support for multiple screens with different sizes and density is very well documented here:
If you are any serious about developing an Android app for more than one type of device, you should have read the above at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.

25 Jun 2015

Android Dimension Description 2

Screen size: Actual physical size, measured as the screen's diagonal. For simplicity, Android groups all actual screen sizes into four generalized sizes: small, normal, large, and extra large.
Screen density: The quantity of pixels within a physical area of the screen; usually referred to as dpi (dots per inch). For example, a "low" density screen has fewer pixels within a given physical area, compared to a "normal" or "high" density screen. For simplicity, Android groups all actual screen densities into four generalized densities: low, medium, high, and extra high.
Orientation: The orientation of the screen from the user's point of view. This is either landscape or portrait, meaning that the screen's aspect ratio is either wide or tall, respectively. Be aware that not only do different devices operate in different orientations by default, but the orientation can change at runtime when the user rotates the device.
Resolution: The total number of physical pixels on a screen. When adding support for multiple screens, applications do not work directly with resolution; applications should be concerned only with screen size and density, as specified by the generalized size and density groups.
Density-independent pixel (dp): A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way. The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.

Android Dimension Description 1

px
Pixels - corresponds to actual pixels on the screen.
in
Inches - based on the physical size of the screen.
1 Inch = 2.54 centimeters
mm
Millimeters - based on the physical size of the screen.
pt
Points - 1/72 of an inch based on the physical size of the screen.
dp
Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".
sp
Scale-independent Pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.