Formula px to dp, dp to px android

AndroidFormulaDensity Independent-Pixel

Android Problem Overview


I am trying to calculate a variable amount of pixels to density independent pixels and vice-versa.

This formula (px to dp): dp = (int)(px / (displayMetrics.densityDpi / 160)); does not work on small devices because it is divided by zero.

This is my dp to px formula:

px = (int)(dp * (displayMetrics.densityDpi / 160));

Could someone give me some pointers?

Android Solutions


Solution 1 - Android

Note: The widely used solution above is based on displayMetrics.density. However, the docs explain that this value is a rounded value, used with the screen 'buckets'. Eg. on my Nexus 10 it returns 2, where the real value would be 298dpi (real) / 160dpi (default) = 1.8625.

Depending on your requirements, you might need the exact transformation, which can be achieved like this:

[Edit] This is not meant to be mixed with Android's internal dp unit, as this is of course still based on the screen buckets. Use this where you want a unit that should render the same real size on different devices.

Convert dp to pixel:

public int dpToPx(int dp) {
	DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
	return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));		
}

Convert pixel to dp:

public int pxToDp(int px) {
	DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
	return Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

Note that there are xdpi and ydpi properties, you might want to distinguish, but I can't imagine a sane display where these values differ greatly.

Solution 2 - Android

I solved my problem by using the following formulas. May other people benefit from it.

dp to px:

displayMetrics = context.getResources().getDisplayMetrics();
return (int)((dp * displayMetrics.density) + 0.5);

px to dp:

displayMetrics = context.getResources().getDisplayMetrics();
return (int) ((px/displayMetrics.density)+0.5);

Solution 3 - Android

Efficient way ever

DP to Pixel:

private int dpToPx(int dp)
{
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

Pixel to DP:

private int pxToDp(int px)
{
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

Hope this will help you.

Solution 4 - Android

px to dp:

int valueInpx = ...;
int valueInDp= (int) TypedValue.applyDimension(
			TypedValue.COMPLEX_UNIT_DIP, valueInpx , getResources()
				.getDisplayMetrics());

Solution 5 - Android

Just call getResources().getDimensionPixelSize(R.dimen.your_dimension) to convert from dp units to pixels

Solution 6 - Android

Use This function

private int dp2px(int dp) {
	return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}

Solution 7 - Android

px = dp * (dpi / 160)

dp = px * (160 / dpi)

Solution 8 - Android

Use these Kotlin extensions:

/**
 * Converts Pixel to DP.
 */
val Int.pxToDp: Int
    get() = (this / Resources.getSystem().displayMetrics.density).toInt()

/**
 * Converts DP to Pixel.
 */
val Int.dpToPx: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()

Solution 9 - Android

In most of the cases, conversion functions are called frequently. We can optimize it by adding memoization. So,it does not calculate every-time the function is called.

Let's declare a HashMap which will store the calculated values.

private static Map<Float, Float> pxCache = new HashMap<>();

A function which calculates pixel values :

public static float calculateDpToPixel(float dp, Context context) {

        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;

    }

A memoization function which returns the value from HashMap and maintains the record of previous values.

Memoization can be implemented in different ways in Java. For Java 7 :

public static float convertDpToPixel(float dp, final Context context) {

        Float f = pxCache.get(dp);
        if (f == null) {
            synchronized (pxCache) {
                f = calculateDpToPixel(dp, context);
                pxCache.put(dp, f);
            }

        }

        return f;
    }

Java 8 supports Lambda function :

public static float convertDpToPixel(float dp, final Context context) {

        pxCache.computeIfAbsent(dp, y ->calculateDpToPixel(dp,context));
}

Thanks.

Solution 10 - Android

Solution 11 - Android

Below funtions worked well for me across devices.

It is taken from https://gist.github.com/laaptu/7867851

public static float convertPixelsToDp(float px){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return Math.round(dp);
}

public static float convertDpToPixel(float dp){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
}

Solution 12 - Android

You can use [DisplayMatrics][1] and determine the screen density. Something like this:

int pixelsValue = 5; // margin in pixels
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(pixelsValue * d);

As I remember it's better to use flooring for offsets and rounding for widths.

Solution 13 - Android

If you're looking for an online calculator for converting DP, SP, inches, millimeters, points or pixels to and from one another at different screen densities, this is the most complete tool I know of.

Solution 14 - Android

// for getting in terms of Decimal/Float

public static float convertPixelsToDp(float px, Context context) {

    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return Math.round(dp);
}



public static float convertDpToPixel(float dp, Context context) {
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
}


// for  getting in terms of Integer

private int convertPxToDp(int px, Context context) {
    Resources resources = context.getResources();
    return Math.round(px / (resources.getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
}


private int convertDpToPx(int dp, Context context) {
    Resources resources = context.getResources();

    return Math.round(dp * (resources.getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));

}

________________________________________________________________________________

public static float convertPixelsToDp(float px){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return Math.round(dp);
}

public static float convertDpToPixel(float dp){
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
    float px = dp * (metrics.densityDpi / 160f);
    return Math.round(px);
}


private int convertDpToPx(int dp){
    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));

}

private int convertPxToDp(int px){
    return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi/DisplayMetrics.DENSITY_DEFAULT));
}

Solution 15 - Android

Elegant kotlin solution :)

val Int.dp get() = this / (Resources.getSystem().displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
val Float.dp get() = this / (Resources.getSystem().displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)

val Int.px get() = this * Resources.getSystem().displayMetrics.density
val Float.px get() = this * Resources.getSystem().displayMetrics.density

Usage:

val dpValue = 2.dp
val pxFromDpValue = 2.px

Improtant:

I am not sure if Resources.getSystem() will work correctly with orientation changes.

If want to work in for example fragment or activity just add it it in to base fragment or base activity and use it like this:

abstract class BaseFragment : Fragment() {

    val Int.dp get() = this / (resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
    val Float.dp get() = this / (resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)

    val Int.px get() = this * resources.displayMetrics.density
    val Float.px get() = this * resources.displayMetrics.density

    .......
}

Solution 16 - Android

Feel free to use this method I wrote:

int dpToPx(int dp)
{
	return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}

Solution 17 - Android

The answer accepted above is not fully accurate. According to information obtained by inspecting Android source code:

Resources.getDimension() and getDimensionPixelOffset()/getDimensionPixelSize() differ only in that the former returns float while the latter two return the same value rounded to int appropriatelly. For all of them, the return value is in raw pixels.

All three functions are implementedy by calling Resources.getValue() and converting thus obtained TypedValue by calling TypedValue.complexToDimension(), TypedValue.complexToDimensionPixelOffset() and TypedValue.complexToDimensionPixelSize(), respectively.

Therefore, if you want to obtain "raw" value together with the unit specified in XML source, call Resources.getValue() and use methods of the TypedValue class.

Solution 18 - Android

DisplayMetrics displayMetrics = contaxt.getResources()
			.getDisplayMetrics();

	int densityDpi = (int) (displayMetrics.density * 160f);
	int ratio = (densityDpi / DisplayMetrics.DENSITY_DEFAULT);
	int px;
	if (ratio == 0) {
		px = dp;
	} else {
		px = Math.round(dp * ratio);

	}

Solution 19 - Android

variation on ct_robs answer above, if you are using integers, that not only avoids divide by 0 it also produces a usable result on small devices:

in integer calculations involving division for greatest precision multiply first before dividing to reduce truncation effects.

px = dp * dpi / 160
dp = px * 160 / dpi

5 * 120 = 600 / 160 = 3

instead of

5 * (120 / 160 = 0) = 0

if you want rounded result do this

px = (10 * dp * dpi / 160 + 5) / 10
dp = (10 * px * 160 / dpi + 5) / 10

10 * 5 * 120 = 6000 / 160 = 37 + 5 = 42 / 10 = 4

Solution 20 - Android

with help of other answers I wrote this function.

public static int convertToPixels(Context context, int nDP)
{
        final float conversionScale = context.getResources().getDisplayMetrics().density; 
        return (int) ((nDP * conversionScale) + 0.5f) ;
}

Solution 21 - Android

Here's a other way to do it using kotlin extensions:

val Int.dpToPx: Int
    get() = Math.round(this * Resources.getSystem().displayMetrics.density)

val Int.pxToDp: Int
    get() = Math.round(this / Resources.getSystem().displayMetrics.density)

and then it can be used like this from anywhere

12.dpToPx

244.pxToDp

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBramView Question on Stackoverflow
Solution 1 - AndroidBachiView Answer on Stackoverflow
Solution 2 - AndroidBramView Answer on Stackoverflow
Solution 3 - AndroidHiren PatelView Answer on Stackoverflow
Solution 4 - AndroidKannan SureshView Answer on Stackoverflow
Solution 5 - AndroidpelotasplusView Answer on Stackoverflow
Solution 6 - AndroidPrashant Maheshwari AndroView Answer on Stackoverflow
Solution 7 - Androidct_robView Answer on Stackoverflow
Solution 8 - AndroidMalwinder SinghView Answer on Stackoverflow
Solution 9 - AndroidParth mehtaView Answer on Stackoverflow
Solution 10 - AndroidKMIView Answer on Stackoverflow
Solution 11 - AndroidBlesson JoseView Answer on Stackoverflow
Solution 12 - AndroidHoucineView Answer on Stackoverflow
Solution 13 - AndroidPaul LammertsmaView Answer on Stackoverflow
Solution 14 - AndroidTrinadh KoyaView Answer on Stackoverflow
Solution 15 - AndroidAntonis RadzView Answer on Stackoverflow
Solution 16 - AndroidBen KaneView Answer on Stackoverflow
Solution 17 - AndroidJaromír AdamecView Answer on Stackoverflow
Solution 18 - AndroidSushil MittalView Answer on Stackoverflow
Solution 19 - Androiduser2624395View Answer on Stackoverflow
Solution 20 - AndroidAbhishekView Answer on Stackoverflow
Solution 21 - AndroidQuinnView Answer on Stackoverflow