Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does @dimen/activity_vertical_margin do?

Tags:

In the following code:

<LinearLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.android.ashokaquiz.MainActivity">

What does @dimen/activity_vertical_margin do? I cannot find any documentation for it. I know what padding is. I just want to know about the @dimen/activity_vertical_margin bit.

Thank you.

like image 424
kudesiaji Avatar asked Dec 28 '16 19:12

kudesiaji


People also ask

What is the best way to set vertical margins?

A better margin strategy is to set all vertical margins in one direction only: either margin-top or margin-bottom. Declaring margin values in this way makes our code much more predictable. You no longer have to worry about the knock-on effect of a collapsed margin.

What does @dimen/activity_vertical_margin mean?

@dimen/activity_vertical_margin or whatever @dimen/whatever_key_name is a reference to a dimension that probably is saved in your projectname/src/main/res/value/dimen.xml file In android you can save several values for example dimensions, strings, integers, drawables...

Why declare margin values as declared?

Declaring margin values in this way makes our code much more predictable. You no longer have to worry about the knock-on effect of a collapsed margin.

Should I apply margins to the top and bottom of elements?

Applying margins to the top and bottom of elements can create layout headaches and maintenance issues. When vertical margins are set in a single direction and combined with the Lobotomized Owl technique, many of these issues are resolved. Found this useful?


1 Answers

@dimen refers to dimension and it's a file where you define dimensions to use them later from in any layout file.

It's located in res/values/dimens. Here's what a sample of the file look like:

 <resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
 </resources>

Here activity_veritcal_margin = 16 dp.

and to use it like this:

<LinearLayout
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin">

Here we give this linear layout a bottom padding with 16dp.

like image 56
zMabrook Avatar answered Sep 21 '22 01:09

zMabrook