14

Is it possible to use the reference of a dimension resource in another dimension? What i mean is something like this:

File dimen.xml:

<dimen name="test1">18sp</dimen>
<dimen name="test2">@dimen/test1</dimen>
STT LCU
  • 4,348
  • 4
  • 29
  • 47
leuchtdiode
  • 477
  • 3
  • 12
  • what would be the purpose of having two dimensions set to the same value? – Roy James Schumacher Jul 25 '13 at 12:52
  • 1
    maybe i want to have test2, test3 and test4 the same value as test1, but use it at different places in code. so if i want to change e.g. the value of test3, i can just change this value and the other ones are untouched – leuchtdiode Jul 25 '13 at 12:55
  • What did the compiler say? – jfs Jul 25 '13 at 13:01
  • actually i didn't try, because i suggest that it won't work?! but i will give this a try. – leuchtdiode Jul 25 '13 at 13:13
  • awesome, it works this way i posted :) – leuchtdiode Jul 25 '13 at 13:17
  • but you cant change the values of the resources once compiled so your reason for wanting duplicate values makes no sense – Roy James Schumacher Jul 25 '13 at 14:41
  • 1
    @RoyJamesSchumacher It makes much sense actually. If you're sure that some dimensions in your layouts will be the same *by definition*, then use the same `dimen`. However, if some dimensions are related in some way and happen to be the same, but *not* by definition, declare 2 dimensions, and refer to the other with `@dimen`. If you want to change the value at some point, the change will be in one place. If during development, you decide it shouldn't be the same value anymore, then change the reference also in one place only. – Joffrey Sep 24 '14 at 07:07

3 Answers3

19

It works the way i posted

<dimen name="test1">18sp</dimen>
<dimen name="test2">@dimen/test1</dimen>
leuchtdiode
  • 477
  • 3
  • 12
2

I would suggest to consider one more thing when using this. I get what you said in your comment, but either there is a link between the two values or there is not and you should distinguish between these two possibilities.

If there is no link between test1 and test2 and they are independent and the fact that they hold the same value is mere coincidence, then you shouldnt suggest a link between the two. Instead just set the same value twice.

Benjamin Basmaci
  • 2,247
  • 2
  • 25
  • 46
-4

Why do you want create a second dimension with the same value as first ?

Don't create second dimension, but use first !

TN888
  • 7,659
  • 9
  • 48
  • 84