Sunday, December 9, 2012


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 is the value to apply the unit to.
metrics is the current display metrics to use in the conversion -- supplies display density and scaling information.

Example:
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <whatever dp value>,
 getResources().getDisplayMetrics());  
That's it, don't hesitate to comment, to share your knowledge and to correct me.