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:
px
Pixels - corresponds to actual pixels on the screen.in
Inches - based on the physical size of the screen.
1 Inch OR 2.54 centimetersmm
> 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 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".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 whatsp
actually stands for, one doc says "scale-independent pixels", the other says "scaleable pixels".
From Understanding Density Independence In Android:
Density Bucket | Screen Density | Physical Size | Pixel Size |
---|---|---|---|
ldpi | 120 dpi | 0.5 x 0.5 in | 0.5 in * 120 dpi = 60x60 px |
mdpi | 160 dpi | 0.5 x 0.5 in | 0.5 in * 160 dpi = 80x80 px |
hdpi | 240 dpi | 0.5 x 0.5 in | 0.5 in * 240 dpi = 120x120 px |
xhdpi | 320 dpi | 0.5 x 0.5 in | 0.5 in * 320 dpi = 160x160 px |
xxhdpi | 480 dpi | 0.5 x 0.5 in | 0.5 in * 480 dpi = 240x240 px |
xxxhdpi | 640 dpi | 0.5 x 0.5 in | 0.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
How to Save a Bitmap on Internal Storage
How to Convert Coordinates of the Image View to the Coordinates of the Bitmap
No Internet on Android Emulator - Why and How to Fix
Android - Zoom In/Out Relativelayout with Spread/Pinch
How to Add a Notification Badge/Count to Application Icon on Sony Xperia Devices
Highlight Text in Textview or Webview
Service Not Available While Calling Geocoder.Getfromlocation()
Android Drawable Images from Url
Can Multiple Android Application Access Same Firebase Database
How to Import Material Design Library to Android Studio
Noclassdeffounderror with Android Studio on Android 4
Android Emulator: Could Not Get Wglgetextensionsstringarb Error
How to Use Asynctask to Show a Progressdialog While Doing Background Work in Android
Share Sqlite Database Between 2 Android Apps
Android: How to Detect Double-Tap
Securityexception: Permission Denied (Missing Internet Permission)