Android Beginner Difference Between Padding And Margin


Answer :

Margin

Margins make up the vertical and horizontal areas between elements. If elements have no margins around them, they will bump right up against each other. In other words, he space outside of, or between, elements is what comprises the margin areas.

Margin

Padding

The padding of an element is the horizontal and vertical space that’s set around the content area of the targeted element. So padding is on the inside of a box, not the outside.

enter image description here


Padding is for inside/within components. Eg. TextView , Button, EditText etc.
Eg. space between the Text and Border

Margin is to be applied for the on-outside of the components.
Eg. space between left edge of the screen and border of your component

Visual representation is great in : Difference between a View's Padding and Margin

With Padding, i have seen a difference in 2.2, 2.3 and say 4.3, 4.4
in such cases:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     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"     tools:context=".MainActivity" >      <Button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_centerHorizontal="true"         android:text="ASDFGHJKL" />      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:paddingLeft="15dp"         android:text="@string/hello_world" />  </RelativeLayout>   

Also, check the use of dimens:
http://developer.android.com/guide/topics/resources/more-resources.html


In Simple words .. if you want to take your widget like TextView, EditText far away from other. You should use margin from top,right,left,bottom.

By increasing padding it will increase the inner spacing not making the widget far apart from others..

enter image description here

Like buttons, for example, the characteristic button background image includes the padding, but not the margin. In other words, adding more padding makes the button look visually bigger, while adding more margin just makes the gap between the button and the next control wider.


Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?