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" /> 36 Answers
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);
When to use dimens.xml
Thanks to this answer for more ideas.
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 singledimenvalue will make it much easier to adjust later. This is the same idea as using styles and themes.Supporting Multiple Screens - A padding of
8dpmight look fine on a phone but terrible on a 10" tablet. You can create multipledimens.xmlto be used with different screens. That way you could do something like set8dpfor the phone and64dpfor the tablet. To create anotherdimens.xmlfile, right click yourresfolder and choose New > Value resource file. (see this answer for details)Convenient
dptopxcode 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 constantdpvalue, you can get it in pixels easy like this forfloat: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.xmlif 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. Usingdimens.xmlmakes 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.xmlbecause 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 adpvalue won't.
add an xml file dimens.xml this is use for support multiple devices.
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="iconarrow">1dp</dimen> <item name="text_view_padding" type="integer">100</item> </resources> then you can use it in your code like this in java code
textview.setPadding(0, 0, 0, getResources().getInteger(R.integer.text_view_padding)); You can also use in other layout(xml file).
android:padding="@dimen/text_view_padding" 1you don't need to mention dimen value in value folder file. this library auto manage all the things you just call like that
<LinearLayout xmlns:android="" xmlns:tools="" xmlns:app="" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/_20sdp" android:paddingLeft="@dimen/_20sdp" android:paddingRight="@dimen/_20sdp" android:paddingTop="@dimen/_20sdp" android:background="@color/colorPrimary" android:orientation="vertical" > whole code click here for that
But about dimensions? According to the official Android docs "A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one <resources> element"
In this post, Devunwired gives three great reasons as to why use dimens.xml When should the dimens.xml file be used in Android?
@Jesús Castro You are doing it right. Maintaining values in the dimens.xml file is better than littering hardcoded values around in all your layout files.
For example, imagine the case where you to increase the left and right margins in all your view. If you used a single value maintained in dimens.xml, this would be a quick change - a single value in a single file. However, if you had put the margin values as a literal values such as "16dp" in your layout files (instead of using a dimens value like "@dimen/leftright_margin"), you have to go edit each layout file which is error prone and just plain time consuming.
I have a novel method I use which I thought is in keeping with the question. I have been avoiding Xml alot to avoid the cost of parsing xml code.
Rather than using xml dimens ,I use java constants. either...
public interface DimenConstants { ... } or...
public class DimenConstants { public static void init(Activity activity){...} } Then in the case of supporting different screen, you can actually do this yourself in Java at runtime. One way is:
public class TestScreenSizes extends Activity { public static final ViewGroup.LayoutParams MAIN_VIEW_SPEC = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); setContentView(newTextView(),MAIN_VIEW_SPEC); } protected TextView newTextView() { TextView tv = new TextView(this); DisplayMetrics display = getResources().getDisplayMetrics(); int resolution = display.widthPixels * display.heightPixels; if(resolution == 1024) tv.setText("You are using an iphone"); else if(resolution == 4096) tv.setText("You are using a Samsung Galexy"); return rv; } }