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.
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.
To create another dimens. xml file, 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).
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.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="my_value">16dp</dimen>
</resources>
Values can be in dp
, px
, or sp
.
Use the value in xml
<TextView
android:padding="@dimen/my_value"
... />
or in code
float sizeInPixels = getResources().getDimension(R.dimen.my_value);
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 single dimen
value will make it much easier to adjust later. This is the same idea as using styles and themes.
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)
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.
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.
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"
you 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="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
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
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