LayoutUtils.java
public abstract class LayoutUtils {
public static int getDisplayWidthPx() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static int getDisplayHeightPx() {
return Resources.getSystem().getDisplayMetrics().heightPixels;
}
public static int getDisplayWidthDp() {
return px2dp(Resources.getSystem().getDisplayMetrics().widthPixels);
}
public static int getDisplayHeightDp() {
return px2dp(Resources.getSystem().getDisplayMetrics().heightPixels);
}
public static int dp2px(int dp){
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
public static int px2dp(int px){
return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}
public static int getPxFromDimension(Context context, @DimenRes int resource) {
return context.getResources().getDimensionPixelSize(resource);
}
public static int getDpFromDimension(Context context, @DimenRes int resource) {
return px2dp(context.getResources().getDimensionPixelSize(resource));
}
}