Java tutorial
//package com.java2s; import android.content.Context; import android.content.res.Resources; import android.util.DisplayMetrics; public class Main { /** * This method converts device specific pixels to device independent pixels. * * @param px A value in px (pixels) unit. Which we need to convert into db * @param context Context to get resources and device specific display metrics * @return A float value to represent db equivalent to px value */ public static float ConvertPixelsToDp(float px, Context context) { Resources resources = context.getResources(); DisplayMetrics metrics = resources.getDisplayMetrics(); float dp = px / (metrics.densityDpi / 160f); return dp; } }