Java tutorial
//package com.java2s; import android.content.Context; import android.content.res.Resources; public class Main { public static int convertPX2DIP(Context context, float px) { Resources resources = context.getResources(); float scale = resources.getDisplayMetrics().density; resources = null; return (int) (px / scale + 0.5f * (px >= 0 ? 1 : -1)); } }