Java tutorial
//package com.java2s; import android.content.Context; import android.util.DisplayMetrics; public class Main { /** * Converts a size value in pixel scale into dp scale. */ public static int pxToDp(Context context, int px) { DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT)); return dp; } }