How do I set Layout and Text size to DP inside program?

You can use:

float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());

Now the value of pixels is equivalent to 10dp at the device’s current screen density.

The TypedValue contains other similar methods that help in conversion.

Leave a Comment