How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android

What is the difference between px, dip, dp, and sp?

From the Android Developer Documentation:


  1. px

    Pixels - corresponds to actual pixels on the screen.


  2. in

    Inches - based on the physical size of the screen.

    1 Inch OR 2.54 centimeters


  3. mm

    > Millimeters - based on the physical size of the screen.


  4. pt

    > Points - 1/72 of an inch based on the physical size of the screen.


  5. dp or dip

    > 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".


  6. sp

    > Scaleable Pixels OR scale-independent pixels - this is like the dp unit, but it is also scaled by the user's font size preference. It is recommended you
    use this unit when specifying font sizes, so they will be adjusted
    for both the screen density and the user's preference. Note, the Android documentation is inconsistent on what sp actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".

From Understanding Density Independence In Android:

















































Density BucketScreen DensityPhysical SizePixel Size
ldpi120 dpi0.5 x 0.5 in0.5 in * 120 dpi = 60x60 px
mdpi160 dpi0.5 x 0.5 in0.5 in * 160 dpi = 80x80 px
hdpi240 dpi0.5 x 0.5 in0.5 in * 240 dpi = 120x120 px
xhdpi320 dpi0.5 x 0.5 in0.5 in * 320 dpi = 160x160 px
xxhdpi480 dpi0.5 x 0.5 in0.5 in * 480 dpi = 240x240 px
xxxhdpi640 dpi0.5 x 0.5 in0.5 in * 640 dpi = 320x320 px

Get screen width and height in Android

Using this code, you can get the runtime display's width & height:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

In a view you need to do something like this:

((Activity) getContext()).getWindowManager()
.getDefaultDisplay()
.getMetrics(displayMetrics);

In some scenarios, where devices have a navigation bar, you have to check at runtime:

public boolean showNavigationBar(Resources resources)
{
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}

If the device has a navigation bar, then count its height:

private int getNavigationBarHeight() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int usableHeight = metrics.heightPixels;
getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
int realHeight = metrics.heightPixels;
if (realHeight > usableHeight)
return realHeight - usableHeight;
else
return 0;
}
return 0;
}

So the final height of the device is:

int height = displayMetrics.heightPixels + getNavigationBarHeight();

Android - Puzzles about conversion between dp and pixel

You are looking at the wrong place in the formula. To see how many dp equals one px in different density, lets rearrange the formula:

px = dp * (dpi/160)
dp = px / (dpi/160)

Now for 1px, in mdpi devices:

dp = 1 / (160/160) = 1dp

In hdpi devices:

dp = 1 / (240/160) = 0.666666667dp

You can see that 1px equals less dp in higher density devices

Android : is a width of 160dp equals to 100% of the screen width?

No. 160 dp is 1 inch (or extremely close). What they're saying is if you had a 160 dpi (dot per inch) device, then 1 pixel=1dp. 160 dpi was common in the early days of Android.



Related Topics



Leave a reply



Submit