When should the dimens.xml file be used in Android?
Asked Answered
V

4

20

For instance, in a specific layout I have the following XML:

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="3dp"
    android:columnWidth="48dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="spacingWidth" />

This grid view is specific to this layout and I don't think I'll be using any other grid views with similar properties. That to say that the dimension values in the code are specific to that grid view.

Should I still move them to a dimens.xml file or it's fine to just leave them like that? If so, should I place values in the dimens.xml file only when that value is used across multiple layouts?

Varipapa answered 22/9, 2011 at 1:16 Comment(0)
L
36

I drop dimension values into a dimens.xml resource typically for three reasons:

  1. Reuse: I need multiple widgets or layouts to use the same value and I only want to change it once when updating or tweaking across the application.

  2. Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi or small -> large.

  3. Reading in from code: When I'm instantiating a view in the code and want to apply some static dimensions, putting them in dimens.xml as dp (or dip) allowing me to get a scaled value in Java code with Resources.getDimensionPixelSize().

Lennielenno answered 22/9, 2011 at 1:33 Comment(8)
About #2, how do you that distinction in the dimens.xml file (provide example if possible)? Reason 3 is very convincing.Varipapa
@Nazgulled multiple files in qualified directories. For instance, putting a dimens.xml in res/values for defaults and another in res/values-ldpi that includes the few I want to override for low density devices.Lennielenno
I though that was only available for the drawables folder. Thanks for the tip.Varipapa
You're welcome. Qualifiers are usable (and extremely useful) on all resource directories. If you look through the AOSP code, you'll see they make heavy use of this themselves as well!Lennielenno
@Devunwired, have you ever had an issue where a high density device references the values-ldpi/dimens.xml file? I have that issue when using the Samsung Galaxy Note - it uses the dimensions defined in values-ldpi/dimens.xml rather than the dimensions defined in values-xxhdpi/dimens.xml OR values-hdpi/dimens.xml Any suggestions?Grewitz
@Enke I have not experienced that problem with the Note. It is an hdpi device and usually picks the correct resources.Lennielenno
@Devunwired, I ended up fixing by removing the dimen elements in the custom values-sw320dpi folder and adding all the missing ones with various dimensions in them, so now I have /res/values ; /values-ldpi, /values-mdpi, /values-hdpi, /values-xhdpi, /values-small, /values-large and /values-xlarge and that seems to work.Grewitz
I expanded your answer below. Feel free to comment.Foreclose
F
24

Supplemental answer

@Devunwired lists 3 reasons to use dimens.xml. Here are the details of how to do that.

1. Reuse

If you set some dp or sp value in dimens.xml once like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_padding">16dp</dimen>
    <dimen name="large_text_size">30sp</dimen>
</resources>

you can reuse it throughout your app in multiple locations.

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

<TextView
    android:padding="@dimen/textview_padding"
    android:textSize="@dimen/large_text_size"
    ... />

Then when you need to make a change, you only need to do it in one place.

Notes

  • This is basically the same effect as using a style or theme.
  • Be careful not to give two different views the same dimen value if they really shouldn't be. If you need to make changes to one set of views but not another, then you will have to go back to each one individually, which defeats the purpose.

2. Size Difference

  • @Devunwired called this Density difference, but if you are using dp (density independent pixels), this already takes care are the density difference problem for all but the most minor cases. So in my opinion, screen size is a more important factor for using dimens.xml.

An 8dp padding might look great on a phone, but when the app is run on a tablet, it looks too narrow. You can solve this problem by making two (or more) different versions of dimens.xml.

Right click your res folder and choose New > Value resource file. Then write in dimens and choose Smallest Screen Width. Write in 600 for the width (7” tablet). (There are other ways of choosing the sizes. See the documentation and this answer for more.)

enter image description here

This will make another values folder that will be used for devices whose smallest screen width is 600dp. In the Android view the two dimens.xml files look like this.

enter image description here

Now you can modify them independently.

values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">16dp</dimen>
</resources>

values-sw600dp/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_default_padding">64dp</dimen>
</resources>

When using your dimen you only have to set it with the name you used in both dimens.xml files.

<LinearLayout
    ...
    android:padding="@dimen/my_default_padding">

</LinearLayout>

The system will automatically choose the right value for you depending on the device the user is using.

3. Reading in from code

Sometimes it is a pain scaling programmatically between px and dp (see this answer for how).

If you have a fixed dp value already defined in dimens.xml like this

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="my_dp_value">16dp</dimen>
</resources>

Then you can easily get it with

int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);

and it will already be converted to pixels for whatever density device the user has.

Foreclose answered 16/11, 2017 at 2:51 Comment(3)
this is very helpful. do you know of a way to force Android Studio to choose a specific dimens.xml for use during layout preview ? I'm having trouble setting up a file for Samsung S7 landscape - I've made a sw640dp values file, but preview is selecting the default.Peachey
@orionelenzil, I don't know about the layout preview. You might try asking a new question about this. I will say that sw600dp is a more standard setting than sw640dp. This is the smallest width, so 600 will include 640 and any other larger sizes. Also, as long as you are getting the correct dimensions when running the app in the emulator or a device, I don't think it really matters if the preview doesn't work.Foreclose
thanks @Suragch. I posted a new question here .Peachey
G
4

The dimens.xml file is used to keep all the hard-coded pixel values in one place.

Now, although you may not repeatedly use these values right now, it's still a good idea to to place them in dimens.xml for future reference. Besides, following a standard Android programming paradigm helps other developers to understand your code faster. This is much like the strings.xml where we place Strings some of which end up being used only once! :)

Gailey answered 22/9, 2011 at 1:29 Comment(4)
It's not quite the same thing, the strings XML files also allows for internationalization, so makes more sense to have them separated from the code.Varipapa
What kind of comment is that?Varipapa
Sorry if I didn't make myself clear. I meant, your comments didn't really help the Original Post's question nor is it entirely relevant to my post. I am just being honest! :)Gailey
Actually it is related, and it's useful. Thats why i voted for his comment. Also, he is the poster of the question. Asking him if he has a better answer for his own question is kinda pointless. Please don't take objective criticism the wrong way. It was just a valid, constructive sidenote, nothing personal. :)Smaragdite
L
0

I don’t know if it can help you but I wrote a little java programe that allows you to duplicate a dimension xml file with a new desired value so that you no longer have to do it by hand line by line.

https://github.com/Drex-xdev/Dimensions-Scalable-Android

Lastditch answered 6/10, 2022 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.