How to Use Dimens.Xml in Android

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.

There is no dimens.xml in android studio project

Your Android Studio is fine. From 2.3, the default Activity layout templates have a ConstraintLayout as their root element with no margins applied to it. In the old templates, this used to be a RelativeLayout with its margins set as resource values in dimens.xml. Since these values are no longer in the default layout file, an empty dimens.xml is not created in the project by default.

If you need a dimens.xml, you can just create one under the res/values folder with New -> Values resource file.


For reference, the old default layout that used dimens resources:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.package.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

</LinearLayout>

And the new default that doesn't:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.package.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

How to define dimens.xml for every different screen size in android?

You have to create Different values folder for different screens .
Like

values-sw720dp          10.1” tablet 1280x800 mdpi

values-sw600dp 7.0” tablet 1024x600 mdpi

values-sw480dp 5.4” 480x854 mdpi
values-sw480dp 5.1” 480x800 mdpi

values-xxhdpi 5.5" 1080x1920 xxhdpi
values-xxxhdpi 5.5" 1440x2560 xxxhdpi

values-xhdpi 4.7” 1280x720 xhdpi
values-xhdpi 4.65” 720x1280 xhdpi

values-hdpi 4.0” 480x800 hdpi
values-hdpi 3.7” 480x854 hdpi

values-mdpi 3.2” 320x480 mdpi

values-ldpi 3.4” 240x432 ldpi
values-ldpi 3.3” 240x400 ldpi
values-ldpi 2.7” 240x320 ldpi

Sample Image

For more information you may visit here

Different values folders in android

http://android-developers.blogspot.in/2011/07/new-tools-for-managing-screen-sizes.html

Edited By @humblerookie

You can make use of Android Studio plugin called Dimenify to auto generate dimension values for other pixel buckets based on custom scale factors. Its still in beta, be sure to notify any issues/suggestions you come across to the developer.

why we refer the dimens.xml file in android with @dimen not @dimens?

name of your files doesn't matter in fact, you can rename dimens.xml to anything.xml. Or you can have dimen_activity.xml and dimen_fragment.xml files, which helps you manage them. Also, you can keep in this file <dimen tags, but also <integers and any other (e.g. you can have one sizes.xml file). Resources are built upon content inside all XML files placed in values, a kind-of map is created then and all <dimens from all XML files are available under @dimen/ or R.dimen.

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

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().

dimen.xml or dimens.xml?

It doesn't really matter what you name the file itself, however there are recommendations. From the docs (on res/values):

Because each resource is defined with its own XML element, you can
name the file whatever you want and place different resource types in
one file. However, for clarity, you might want to place unique
resource types in different files. For example, here are some filename
conventions
for resources you can create in this directory:

  • arrays.xml for resource arrays (typed arrays).
  • colors.xml for color values
  • dimens.xml for dimension values.
  • strings.xml for string values.
  • styles.xml for styles.

Edit: For clarity, the important part is that you use the appropriate element (<dimen> in this case) inside a <resource> tag in your file. This is how android knows it's a dimension resource.

<resources>
<dimen name="my_dimen">10dip</dimen>
</resources>


Related Topics



Leave a reply



Submit