I think the best way is not to define any dimensions in code, but use the values/dimens.xml file instead. You can always define your dimension in the desired unit like:
<dimen name="my_layout_height">120dp</dimen> and then refer to this in your Activity like:
getResources().getDimensions(R.dimen.my_layout_height); This would return pixels after doing the necessary conversions. And also of course you can refer to this in your other XMLs like:
android:layout_width="@dimen/my_layout_height"