Java tutorial
//package com.java2s; import android.content.Context; public class Main { public static int convertPxToDp(Context context, int px) { float d = context.getResources().getDisplayMetrics().density; return (int) ((px / d) + 0.5); } }