You can use DisplayMetrics and determine the screen density. Something like this:
int dpValue = 5; // margin in dips
float d = context.getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d); // margin in pixels
As I remember it’s better to use flooring for offsets and rounding for widths.