For instance, in a specific layout I have the following XML:
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:columnWidth="48dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="spacingWidth" />
This grid view is specific to this layout and I don't think I'll be using any other grid views with similar properties. That to say that the dimension values in the code are specific to that grid view.
Should I still move them to a dimens.xml file or it's fine to just leave them like that? If so, should I place values in the dimens.xml file only when that value is used across multiple layouts?
dimens. xml is used for defining the dimensions for different widgets to be included in the Android project. It is a good coding practice to use dimens.
Note: 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.
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 . true but there what is the cause of it and how to get it created automatically when the project is created.
The dimens.xml file is used to keep all the hard-coded pixel values in one place. Now, although you may not repeatedly use these values right now, it's still a good idea to to place them in dimens.xml for future reference. Besides, following a standard Android programming paradigm helps other developers to understand your code faster.
Different XML Files Used in Android: In Android there are several xml files used for several different purposes. Below we define each and every one. 1. Layout XML Files: Layout xml files are used to define the actual UI(User interface) of our application. It holds all the elements(views) or the tools that we want to use in our application.
How to use dimens.xml. 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.) Add a dimen name and value.
In the Android view the two dimens.xml files look like this. Now you can modify them independently. When using your dimen you only have to set it with the name you used in both dimens.xml files. The system will automatically choose the right value for you depending on the device the user is using.
I drop dimension values into a dimens.xml
resource typically for three reasons:
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.
Density Difference: If I need the dimension to be slightly smaller or larger from ldpi -> hdpi
or small -> large
.
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()
.
@Devunwired lists 3 reasons to use dimens.xml
. Here are the details of how to do that.
If you set some dp
or sp
value in dimens.xml
once like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_padding">16dp</dimen>
<dimen name="large_text_size">30sp</dimen>
</resources>
you can reuse it throughout your app in multiple locations.
<TextView
android:padding="@dimen/textview_padding"
android:textSize="@dimen/large_text_size"
... />
<TextView
android:padding="@dimen/textview_padding"
android:textSize="@dimen/large_text_size"
... />
Then when you need to make a change, you only need to do it in one place.
Notes
dimen
value if they really shouldn't be. If you need to make changes to one set of views but not another, then you will have to go back to each one individually, which defeats the purpose.dp
(density independent pixels), this already takes care are the density difference problem for all but the most minor cases. So in my opinion, screen size is a more important factor for using dimens.xml
.An 8dp
padding might look great on a phone, but when the app is run on a tablet, it looks too narrow. You can solve this problem by making two (or more) different versions of dimens.xml
.
Right click your res
folder and choose New > Value resource file. Then write in dimens
and choose Smallest Screen Width. Write in 600 for the width (7” tablet). (There are other ways of choosing the sizes. See the documentation and this answer for more.)
This will make another values
folder that will be used for devices whose smallest screen width is 600dp
. In the Android view the two dimens.xml
files look like this.
Now you can modify them independently.
values/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">16dp</dimen>
</resources>
values-sw600dp/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_default_padding">64dp</dimen>
</resources>
When using your dimen
you only have to set it with the name you used in both dimens.xml
files.
<LinearLayout
...
android:padding="@dimen/my_default_padding">
</LinearLayout>
The system will automatically choose the right value for you depending on the device the user is using.
Sometimes it is a pain scaling programmatically between px
and dp
(see this answer for how).
If you have a fixed dp
value already defined in dimens.xml
like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_dp_value">16dp</dimen>
</resources>
Then you can easily get it with
int sizeInPixels = getResources().getDimensionPixelSize(R.dimen.my_dp_value);
and it will already be converted to pixels for whatever density device the user has.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With