Java tutorial
//package com.java2s; import android.app.Activity; import android.util.DisplayMetrics; public class Main { /** * Pixels to dp. * * @param context the context * @param px the px * @return the int */ public static int PixelsToDp(Activity context, int px) { DisplayMetrics metrics = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(metrics); return (int) ((px / metrics.density) + 0.5); } }