Java tutorial
//package com.java2s; import android.content.Context; public class Main { public static float density = 1; public static float dpFromPx(final Context context, final float px) { return px / context.getResources().getDisplayMetrics().density; } }