Android How to - Get pixel size by dip value








Question

We would like to know how to get pixel size by dip value.

Answer


import android.content.Context;
import android.util.TypedValue;

public class Main {
  public static float getPX(Context context, int dipValue){
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, context.getResources().getDisplayMetrics());
  }
}