Convert Pixels to Sp

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:


  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:



Leave a reply



Submit