Reputation: 4405
Is there any reason to avoid having dp
s defined directly in the xml files?
E.g. is there any reason to prefer:
android:layout_marginLeft="@dimen/left_margin"
over this:
android:layout_marginLeft=16dp
If I understand correctly, this would make sense only for tablets but in that case won't we have a relevant xml in land
with the relevant values?
Also if is there anything to be careful in regards to adding something to dimen.xml
? E.g. should left_margin
be defined in all dimen.xml
for all dimension?
Upvotes: 0
Views: 62
Reputation: 17834
One good reason would be that maybe you have a whole lot of layouts where marginLeft
needs to be the same. You could set them all to 16dp
right in the layout files, but what if you need to change that dimension? If you have 16dp
defined in every layout, you'll have to change every single instance of it, and you might forget some. If it's defined in dimens
you only have to change it once.
Another reason would be themers. Someone might make a theme for your app. It's a lot easier to override extracted values than having to override and copy an entire layout just to change one value.
And as Gabe says, you really should use start
and end
instead of left
and right
, whenever possible. Otherwise, your app will look terrible on devices using RTL.
Upvotes: 1
Reputation: 93561
There's a good reason for using the first. Lets say that you decide on really big devices you want a bigger margin. With the first, you only have to override the dimens file and override the 1 dimension. In the second, you have to override the entire layout, which causes larger maintenance issues.
(Also you should almost always be using marginStart and marginEnd instead of left and right, so you handle RTL languages correctly).
Upvotes: 2