How to Use Dimens. Xml in Android?
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...
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" />
6 Answers
Must Read
How to use dimens.xml
Create a new
dimens.xmlfile by right clicking thevaluesfolder and choosing New > Values resource file. Writedimensfor the name. (You could also call itdimenordimensions. The name doesn't really matter, only thedimenresource type that it will include.)Add a
dimenname and value.<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="my_value">16dp</dimen> </resources>Values can be in
dp,px, orsp.Use the value in xml
<TextView android:padding="@dimen/my_value" ... />or in code
float sizeInPixels = getResources().getDimension(R.dimen.my_value);