Storing Dimensions in Xml File in Android

Storing dimensions in xml file in Android

Create a file called res/values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="icon_width">55dip</dimen>
<dimen name="icon_height">55dip</dimen>
<dimen name="photo_width">170dip</dimen>
<dimen name="photo_height">155dip</dimen>
</resources>

Then reference them in your other xml:

android:padding="@dimen/icon_width">

How to use dimens.xml in Android?


How to use dimens.xml

  1. Create a new dimens.xml file by right clicking the values folder and choosing New > Values resource file. Write dimens for the name. (You could also call it dimen or dimensions. The name doesn't really matter, only the dimen resource type that it will include.)

  2. Add a dimen name and value.

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

    Values can be in dp, px, or sp.

  3. Use the value in xml

     <TextView
    android:padding="@dimen/my_value"
    ... />

    or in code

     float sizeInPixels = getResources().getDimension(R.dimen.my_value);

When to use dimens.xml

Thanks to this answer for more ideas.

  1. Reusing values - If you need to use the same dimension multiple places throughout your app (for example, Activity layout padding or a TextView textSize), then using a single dimen value will make it much easier to adjust later. This is the same idea as using styles and themes.

  2. Supporting Multiple Screens - A padding of 8dp might look fine on a phone but terrible on a 10" tablet. You can create multiple dimens.xml to be used with different screens. That way you could do something like set 8dp for the phone and 64dp for the tablet. To create another dimens.xml file, right click your res folder and choose New > Value resource file. (see this answer for details)

  3. Convenient dp to px code conversion - In code you usually need to work with pixel values. However you still have to think about the device density and the conversion is annoying to do programmatically. If you have a constant dp value, you can get it in pixels easy like this for float:

     float sizeInPixels = getResources().getDimension(R.dimen.my_value);

    or this for int :

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

I give many more details of how to do these things in my fuller answer.

When not to use dimens.xml

  • Don't put your values in dimens.xml if it is going to make them more difficult to maintain. Generally that will be whenever it doesn't fall into the categories I listed above. Using dimens.xml makes the code harder to read because you have to flip back and forth between two files to see what the actual values are. It's not worth it (in my opinion) for individual Views.

  • Strings are different. All strings should go in a resource file like strings.xml because almost all strings need to be translated when internationalizing your app. Most dimension values, on the other hand, do not need to change for a different locality. Android Studio seems to support this reasoning. Defining a string directly in the layout xml will give a warning but defining a dp value won't.

Using FILL_PARENT in dimensions.xml

You should use styles for different screens. Like this:

 <View
style="@style/MyStyle"
android:layout_height="100dip"/>

in values-sw600dp/my_style.xml:

    <style name="MyStyle">
<item name="android:layout_width">600dip</item>
</style>

in values/my_style.xml:

    <style name="MyStyle">
<item name="android:layout_width">match_parent</item>
</style>

With this you can do what you want.

How to store ints for different screen sizes in a .xml?

Sure, you can do that. Just like storing a string Ressource you can store Integers.

Taken from the documentation:

XML file saved at res/values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="big_screen_width">12000</integer>
<integer name="small_screen_width">320</integer>
</resources>

And the code would look like that

Resources res = getResources();
int bigScreen = res.getInteger(R.integer.big_screen_width);

Get integer value from dimens.xml resource file in Android

Why not store the integer in res/integers.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="quantity_length">12</integer>
</resources>

And to access the values in code

int myInteger = getResources().getInteger(R.integer.quantity_length);

Or in XML

android:maxLength="@integer/quantity_length"

android : style.xml or dimens.xml or what else? confusing


Keep style.xml and dimens.xml separate ?

As soon as style.xml already exists for reasons like these (button colors, text color, themes, etc.) then I believe that you should keep them separate.

And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?

I would do the (2). (Although I can't blame anyone who would do the (1).). As far as I know there's no best practice guidance on that. I do it the (2) way basically because I see dimen.xml as a place to host pixel-related values. But this is my preference.

How to create a folder called dimens in Android Studio under Values folder?

As Blackbelt said, you can't have folders inside "values" folder, and therefore can't create "dimens" folder inside "values" folder.

You can only create dimens xml file for storing dimension values, and You create it in "values" folder.

I guess You are following some tutorial...if that's true, then it's probably typing mistake.

Set text size in .xml or programmatically

setTextSize( float ) expects a scaled pixel value. So, setTextSize( 12 ) would give you the desired result. However, getDimension() and getDimensionPixelSize() return the size in units of pixels, so you need to use the unit-typed variant of setTextSize() as follows:

setTextSize( TypedValue.COMPLEX_UNIT_PX, getDimensionPixelSize( R.dimen.tag_text_size ) );


Related Topics



Leave a reply



Submit