Issue
In my Android project I have multiple values folders with extensions such as -hdpi
, -xhdpi
, and -sw720dp
. In all of these folders I have dimens.xml
and the same names for values in that xml file. If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from? Also, in general, how does Android prioritize the different values folders?
Solution
Also, in general, how does Android prioritize the different values folders?
Generally speaking, Android takes the resources from the resource set that has the resource in question and has the most important distinct qualifier. Here, "most important" means "appears highest in Table 2 on this Web page", and "distinct" means "nobody else has that particular qualifier". Though things get a bit weird with screen sizes and densities.
I have multiple values folders with extensions such as -hdpi, -xhdpi, and -sw720dp. In all of these folders I have dimens.xml and the same names for values in that xml file.
IMHO that is a serious code smell. Use density-independent units of measure (e.g., dp
), in which case you should not need different dimension resources based upon screen density. Having different ones based upon screen size is reasonable (e.g., bigger margins on tablets), though the mix then would be something like res/values/dimens.xml
and res/values-sw720dp/values.xml
.
If I have a device that has a width of 720 dp and is counted as an xhdpi device (as an example), then from what folder does Android read the values from?
-swNNNdp
is higher in Table 2 than is -hdpi
or -xhdpi
, and so it should be considered first. Note that the size resource set qualifiers (e.g., -large
, -swNNNdp
) are for the specified size or larger. So, if your device has 720dp or more in the smallest width, your -sw720dp
resource should be used.
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.