How to use dimens.xml in Android?

Issue

When I design a layout, I centralize all dimensions in dimens.xml because of topics of maintainability. My question is if this is correct or not. What would it be the best good practice? There is very little information about this, nothing. I know it’s good idea to centralize all strings of a layout on strings.xml, colors on colors.xml. But about dimensions?

For example:

<TableLayout
    android:id="@+id/history_detail_rows_submitted"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/cebroker_history_detail_rows_border"
    android:collapseColumns="*">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/history_detail_rows_margin_vertical"
        android:background="@color/cebroker_history_detail_rows_background"
        android:gravity="center"
        android:paddingBottom="@dimen/history_detail_rows_padding_vertical"
        android:paddingLeft="@dimen/history_detail_rows_padding_horizontal"
        android:paddingRight="@dimen/history_detail_rows_padding_horizontal"
        android:paddingTop="@dimen/history_detail_rows_padding_vertical">

        <TextView
            android:layout_width="match_parent"
            android:drawableLeft="@mipmap/ic_history_detail_submitted_by"
            android:drawablePadding="@dimen/history_detail_rows_textviews_padding_drawable"
            android:gravity="left|center"
            android:paddingRight="@dimen/history_detail_rows_textviews_padding"
            android:text="@string/history_detail_textview_submitted_by"
            android:textColor="@color/cebroker_history_detail_rows_textviews"
            android:textSize="@dimen/history_detail_rows_textviews_text_size" />

Solution

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.

Answered By – Suragch

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *