Java tutorial
//package com.java2s; import android.content.Context; import android.util.DisplayMetrics; import android.view.WindowManager; public class Main { public static float pixelsToDp(Context ctx, float px) { WindowManager wm = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)); DisplayMetrics metrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); float logicalDensity = metrics.density; return (float) Math.ceil(px * logicalDensity); } }