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