Hola,
It's highly recommended to use (dp/dip) when defining UI layout, to express layout dimensions or position in a density-independent way.But, how can we specify the dimensions in (dp) programmatically?
The answer is:
float android.util.TypedValue.applyDimension(int unit, float value,
DisplayMetrics metrics)
Where (as mentioned in the documentation):
unit is the unit to convert from.
value...
Android, density independent pixel (dp) to pixels(px)