How to Convert Dp, Px, Sp Among Each Other, Especially Dp and Sp

How to convert DP, PX, SP among each other, especially DP and SP?

DP to PX:

public static int dpToPx(float dp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

SP to PX:

public static int spToPx(float sp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}

DP to SP:

public static int dpToSp(float dp, Context context) {
return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity);
}

Convert sp to pixels programmatically on android

You can use the Apply Dimen using TypeValue, like below.

public int spToPx(float sp, Context context) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
}

It's an old question, you may found a lot of discussion about this

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

Convert pixels to sp

See the DisplayMetrics class, it has fields for densityDpi and scaledDensity.

Example usage:

float sp = px / getResources().getDisplayMetrics().scaledDensity;

Convert Pixel to Dp for Custom Rounded Indication Clickable in Jetpack Compose

As described by 2jan222 you can convert using Dp.toPx().

In your case you could avoid to build a custom Indication.

You could use something like:

 val interactionSource = remember { MutableInteractionSource() }
Modifier.clickable(
interactionSource = interactionSource,
indication = rememberRipple(
radius = 4.dp,
color=Color.Black.copy(alpha = 0.3f))
)

Converting automatically from px to sp or dp in Android

Unfortunately, px to dp conversion can not be done by the system automatically for you.

however, you can define several values folders for various screen sizes and define different dimensions(in dp) for each of them.

refer this question for more details:

How to define dimens.xml for every different screen size in android?

Conversion from PX to dp or sp is something you have to do yourself.

and to conclude, you should not be doing this as most of the time values defined in dp and sp suits to most of the screen sizes, so try that first.

Accurate conversion of dp to px in android programatically

the difference is in rounding value and truncating value.

Rounding of 4.6 will result in 5

Truncating 4.6 will result in 4.

It really does not relate to Android, and basically is Algebra, not a programming question.

How to use dp instead of pixels in RecyclerView.ItemDecoration()?

by this method you should first convert dp to pixels than set

fun Context.dpToPixel(dp: Float): Float {
return dp * (resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
}

by this

addItemDecoration(SpacesItemDecoration(dpToPixel(25f)))


Related Topics



Leave a reply



Submit