Convert pixels to sp
See the DisplayMetrics class, it has fields for densityDpi
and scaledDensity
.
Example usage:
float sp = px / getResources().getDisplayMetrics().scaledDensity;
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 40px to sp
case COMPLEX_UNIT_SP:
return value * metrics.scaledDensity;
from the TypedValue.applyDimension() source
This converts sp to px, just transpose it and you get the formula for converting px to sp:
sp = px / DisplayMetrics.scaledDensity
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.
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 |
Related Topics
How to Monitor Sim State Change
How to Change the Font Size of Tabhost in Android
Android Webview Click Open Within Webview Not a Default Browser
Proguard with Ormlite on Android
Firebase Onmessagereceived Not Called When App Is in the Background
Error Inflating Class and Android.Support.V7.Widget.Cardview
Error: Failed to Resolve: Com.Android.Support:Appcompat-V7:29.0.1'
Understanding Android: Zygote and Dalvikvm
How to Use Room Persistence Library with Pre-Populated Database
Android Event for Internet Connectivity State Change
How to Display More Than 3- Levels of Expandable List View
List View Items Changes Position When Scrolling Android
Validation Allow Only Number and Characters in Edit Text in Android
Continuous Speech Recognition Android
Android Bluetoothdevice.Getname() Return Null
Google Maps API V2 'Failed to Load Map. Could Not Contact Google Servers'