For instance, in a specific layout I have the following XML:
I drop dimension values into a dimens.xml
resource typically for three reasons:
Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.
Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi
or small -> large
.
Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in dimens.xml
as dp
(or dip
) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize()
.