Should we avoid the size definition inside the xml?
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?
android android-layout android-xml android-resources android-screen-support
add a comment |
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?
android android-layout android-xml android-resources android-screen-support
Ifleft_margin
is met more than 2 times it should go todimen.xml
, otherwise16dp
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) severalxml
layout files. Why modifying the value in multiple files instead of one (dimen.xml
)?!
– Onik
Nov 15 '18 at 20:58
add a comment |
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?
android android-layout android-xml android-resources android-screen-support
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?
android android-layout android-xml android-resources android-screen-support
android android-layout android-xml android-resources android-screen-support
asked Nov 15 '18 at 20:30
JimJim
33518
33518
Ifleft_margin
is met more than 2 times it should go todimen.xml
, otherwise16dp
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) severalxml
layout files. Why modifying the value in multiple files instead of one (dimen.xml
)?!
– Onik
Nov 15 '18 at 20:58
add a comment |
Ifleft_margin
is met more than 2 times it should go todimen.xml
, otherwise16dp
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) severalxml
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
add a comment |
2 Answers
2
active
oldest
votes
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).
For the really big devices, would I need a seconddimens.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
add a comment |
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.
Sostart
andend
is forLinearLayout
too?
– Jim
Nov 15 '18 at 20:57
Any time you could usestart
orend
in place ofleft
orright
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>
orscreen_left_margin
orleft_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 itleft_margin_16
then I can't just change from16dp
to e.g.22dp
and have the_16
in the name. Also theleft_margin
is too generic. What if anotherleft_margin
of22dp
was needed? Do I make sense?
– Jim
Nov 15 '18 at 22:14
|
show 1 more comment
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
For the really big devices, would I need a seconddimens.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
add a comment |
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).
For the really big devices, would I need a seconddimens.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
add a comment |
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).
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).
answered Nov 15 '18 at 20:33
Gabe SechanGabe Sechan
68k965100
68k965100
For the really big devices, would I need a seconddimens.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
add a comment |
For the really big devices, would I need a seconddimens.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
add a comment |
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.
Sostart
andend
is forLinearLayout
too?
– Jim
Nov 15 '18 at 20:57
Any time you could usestart
orend
in place ofleft
orright
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>
orscreen_left_margin
orleft_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 itleft_margin_16
then I can't just change from16dp
to e.g.22dp
and have the_16
in the name. Also theleft_margin
is too generic. What if anotherleft_margin
of22dp
was needed? Do I make sense?
– Jim
Nov 15 '18 at 22:14
|
show 1 more comment
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.
Sostart
andend
is forLinearLayout
too?
– Jim
Nov 15 '18 at 20:57
Any time you could usestart
orend
in place ofleft
orright
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>
orscreen_left_margin
orleft_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 itleft_margin_16
then I can't just change from16dp
to e.g.22dp
and have the_16
in the name. Also theleft_margin
is too generic. What if anotherleft_margin
of22dp
was needed? Do I make sense?
– Jim
Nov 15 '18 at 22:14
|
show 1 more comment
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.
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.
answered Nov 15 '18 at 20:35
TheWandererTheWanderer
7,66021230
7,66021230
Sostart
andend
is forLinearLayout
too?
– Jim
Nov 15 '18 at 20:57
Any time you could usestart
orend
in place ofleft
orright
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>
orscreen_left_margin
orleft_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 itleft_margin_16
then I can't just change from16dp
to e.g.22dp
and have the_16
in the name. Also theleft_margin
is too generic. What if anotherleft_margin
of22dp
was needed? Do I make sense?
– Jim
Nov 15 '18 at 22:14
|
show 1 more comment
Sostart
andend
is forLinearLayout
too?
– Jim
Nov 15 '18 at 20:57
Any time you could usestart
orend
in place ofleft
orright
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>
orscreen_left_margin
orleft_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 itleft_margin_16
then I can't just change from16dp
to e.g.22dp
and have the_16
in the name. Also theleft_margin
is too generic. What if anotherleft_margin
of22dp
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
|
show 1 more comment
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
If
left_margin
is met more than 2 times it should go todimen.xml
, otherwise16dp
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