Should we avoid the size definition inside the xml?










0















Is there any reason to avoid having dps 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?










share|improve this question






















  • If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

    – Onik
    Nov 15 '18 at 20:33











  • @Onik: Is set more than 2 times where? I didn't understand

    – Jim
    Nov 15 '18 at 20:55











  • Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

    – Onik
    Nov 15 '18 at 20:58















0















Is there any reason to avoid having dps 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?










share|improve this question






















  • If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

    – Onik
    Nov 15 '18 at 20:33











  • @Onik: Is set more than 2 times where? I didn't understand

    – Jim
    Nov 15 '18 at 20:55











  • Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

    – Onik
    Nov 15 '18 at 20:58













0












0








0








Is there any reason to avoid having dps 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?










share|improve this question














Is there any reason to avoid having dps 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?







android android-layout android-xml android-resources android-screen-support






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 20:30









JimJim

33518




33518












  • If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

    – Onik
    Nov 15 '18 at 20:33











  • @Onik: Is set more than 2 times where? I didn't understand

    – Jim
    Nov 15 '18 at 20:55











  • Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

    – Onik
    Nov 15 '18 at 20:58

















  • If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

    – Onik
    Nov 15 '18 at 20:33











  • @Onik: Is set more than 2 times where? I didn't understand

    – Jim
    Nov 15 '18 at 20:55











  • Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

    – Onik
    Nov 15 '18 at 20:58
















If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

– Onik
Nov 15 '18 at 20:33





If left_margin is met more than 2 times it should go to dimen.xml, otherwise 16dp is fine.

– Onik
Nov 15 '18 at 20:33













@Onik: Is set more than 2 times where? I didn't understand

– Jim
Nov 15 '18 at 20:55





@Onik: Is set more than 2 times where? I didn't understand

– Jim
Nov 15 '18 at 20:55













Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

– Onik
Nov 15 '18 at 20:58





Sorry for describing it briefly. I meant if it's met more than twice in one or (moreover) several xml layout files. Why modifying the value in multiple files instead of one (dimen.xml)?!

– Onik
Nov 15 '18 at 20:58












2 Answers
2






active

oldest

votes


















2














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).






share|improve this answer























  • For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

    – Jim
    Nov 15 '18 at 20:56











  • Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

    – Gabe Sechan
    Nov 15 '18 at 21:01


















1














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.






share|improve this answer























  • So start and end is for LinearLayout too?

    – Jim
    Nov 15 '18 at 20:57











  • Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

    – TheWanderer
    Nov 15 '18 at 20:58











  • I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

    – Jim
    Nov 15 '18 at 21:02











  • You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

    – TheWanderer
    Nov 15 '18 at 21:04











  • If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

    – Jim
    Nov 15 '18 at 22:14










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327463%2fshould-we-avoid-the-size-definition-inside-the-xml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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).






share|improve this answer























  • For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

    – Jim
    Nov 15 '18 at 20:56











  • Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

    – Gabe Sechan
    Nov 15 '18 at 21:01















2














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).






share|improve this answer























  • For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

    – Jim
    Nov 15 '18 at 20:56











  • Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

    – Gabe Sechan
    Nov 15 '18 at 21:01













2












2








2







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).






share|improve this answer













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).







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:33









Gabe SechanGabe Sechan

68k965100




68k965100












  • For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

    – Jim
    Nov 15 '18 at 20:56











  • Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

    – Gabe Sechan
    Nov 15 '18 at 21:01

















  • For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

    – Jim
    Nov 15 '18 at 20:56











  • Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

    – Gabe Sechan
    Nov 15 '18 at 21:01
















For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

– Jim
Nov 15 '18 at 20:56





For the really big devices, would I need a second dimens.xml in a specific directory? What happens if that is missing?

– Jim
Nov 15 '18 at 20:56













Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

– Gabe Sechan
Nov 15 '18 at 21:01





Then it would fall back to the default dimens file. This is about being able to customize for different conditions with the minimum effort.

– Gabe Sechan
Nov 15 '18 at 21:01













1














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.






share|improve this answer























  • So start and end is for LinearLayout too?

    – Jim
    Nov 15 '18 at 20:57











  • Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

    – TheWanderer
    Nov 15 '18 at 20:58











  • I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

    – Jim
    Nov 15 '18 at 21:02











  • You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

    – TheWanderer
    Nov 15 '18 at 21:04











  • If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

    – Jim
    Nov 15 '18 at 22:14















1














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.






share|improve this answer























  • So start and end is for LinearLayout too?

    – Jim
    Nov 15 '18 at 20:57











  • Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

    – TheWanderer
    Nov 15 '18 at 20:58











  • I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

    – Jim
    Nov 15 '18 at 21:02











  • You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

    – TheWanderer
    Nov 15 '18 at 21:04











  • If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

    – Jim
    Nov 15 '18 at 22:14













1












1








1







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:35









TheWandererTheWanderer

7,66021230




7,66021230












  • So start and end is for LinearLayout too?

    – Jim
    Nov 15 '18 at 20:57











  • Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

    – TheWanderer
    Nov 15 '18 at 20:58











  • I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

    – Jim
    Nov 15 '18 at 21:02











  • You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

    – TheWanderer
    Nov 15 '18 at 21:04











  • If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

    – Jim
    Nov 15 '18 at 22:14

















  • So start and end is for LinearLayout too?

    – Jim
    Nov 15 '18 at 20:57











  • Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

    – TheWanderer
    Nov 15 '18 at 20:58











  • I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

    – Jim
    Nov 15 '18 at 21:02











  • You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

    – TheWanderer
    Nov 15 '18 at 21:04











  • If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

    – Jim
    Nov 15 '18 at 22:14
















So start and end is for LinearLayout too?

– Jim
Nov 15 '18 at 20:57





So start and end is for LinearLayout too?

– Jim
Nov 15 '18 at 20:57













Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

– TheWanderer
Nov 15 '18 at 20:58





Any time you could use start or end in place of left or right you should. It doesn't only apply to margins, but also padding, relations, gravity, etc.

– TheWanderer
Nov 15 '18 at 20:58













I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

– Jim
Nov 15 '18 at 21:02





I get the point about instead of changing it in every layout file, change it once, but then what is the convention to use as a name? I can't name it e.g. <dimen name="left_margin">16dp</dimen> or screen_left_margin or left_margin_16. So this is confusing to me

– Jim
Nov 15 '18 at 21:02













You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

– TheWanderer
Nov 15 '18 at 21:04





You can name it whatever you want. I usually go for something descriptive, so I know what it's for.

– TheWanderer
Nov 15 '18 at 21:04













If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

– Jim
Nov 15 '18 at 22:14





If it is meant to be reused across multiple layout files (=views) if a reference to a specific view is mentioned in then name then it seems wrong. If I name it left_margin_16 then I can't just change from 16dp to e.g. 22dp and have the _16 in the name. Also the left_margin is too generic. What if another left_margin of 22dp was needed? Do I make sense?

– Jim
Nov 15 '18 at 22:14

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327463%2fshould-we-avoid-the-size-definition-inside-the-xml%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党