Java tutorial
//package com.java2s; //License from project: Open Source License import android.content.Context; public class Main { public static final int getWidthInDp(Context context) { final float width = context.getResources().getDisplayMetrics().widthPixels; int widthInDp = px2dip(context, width); return widthInDp; } public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } }