Formula px to dp and dp to px conversion

suggest change

density is the scale from dp to px. The int cast drops the fraction. Resources.getSystem() uses the system display, which can differ from an activity configuration.

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);
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents