55

I have referred questions on SO. ALso checked an answer:
Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).

Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.

Also, more on padding and margins from:

http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

But what is the difference fundamentally between padding and margins ? Would the behaviour differ depending on O.S. and devices?
I have a normal, simple layout. No problem with code, have used layout folders - layout and layout-sw600dp plus drawables-4dpi. Can't make layout without margin or padding, which one is more appropriate?

  • 6
    Padding is an **inner** spacing, while margin is an **outer** spacing. That's all. I suppose you are referring to the extra padding introduced by the newer OS themes? You can assign a different theme and/or use negative padding, to fix that. – Phantômaxx Feb 22 '14 at 19:09
  • 2
    extending @ArtooDetoo's comment see this answer, it will remove all your doubts http://stackoverflow.com/a/4619943/1979347 – Rohan Kandwal Feb 22 '14 at 19:11

9 Answers9

74

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

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
47

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

Pararth
  • 8,114
  • 4
  • 34
  • 51
  • thank you , yes i will use dimen , had not come across that. and saw the difference is substantial in the relative layout example for os 2.3, and 4.3 , 4.0! have seen the link you mentioned –  Feb 22 '14 at 19:30
29

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.

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
25

Margin: Between border and its parent layout

Padding: Between content and border

refer to this

enter image description here

Community
  • 1
  • 1
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
  • thank you, yes i saw that on http://stackoverflow.com/questions/4619899/difference-between-a-views-padding-and-margin –  Feb 22 '14 at 19:24
2

The difference between margin and padding and use cases are clearly explained by +Nick Butcher in Udacity's video. Here's the excerpt:

...if you want the touchable area or the background of the object to be enlarged, then use padding, otherwise use margin...

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
1

what is the difference fundamentally between padding and margins ?

For the differences - Rohan Khandwal has shared a very perfect link.

Would the behaviour differ depending on O.S. and devices?

Now If we are talking about the behaviour of the view which has been given diffrent margins & padding. Then It will definitely look diffrent in different devices with diffrent resolutions. Thats why we are given diffrent dimen/values/layout folders which have their own meanings.

Sagar Shah
  • 4,272
  • 2
  • 25
  • 36
  • thank you, yes that is what i wanted to know. I checked the example in answer by @user2450263 , the padding difference is substantial in 2.3 and 4.3 and 4.0! –  Feb 22 '14 at 19:28
  • Yes thats also useful links. – Sagar Shah Feb 22 '14 at 19:31
0

The difference between android margin and padding is that even though the text is how much sp you want away from the edge, margin is not spaced or colored in. It is only the text and the color you set with it all "alone". With padding though, the text is away from the edge of the screen just like margin but, in padding the text is away and all the space between the text and the edge of the screen is filled in with the color or any text preference you chose to be. This is the difference between android margin and android padding.

Samarth Shah
  • 941
  • 1
  • 8
  • 7
0

Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent).

Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object. Note that, like the padding, the margin goes completely around the content: there are margins on the top, bottom, right, and left sides.

Padding Increases the size of the view where as margin doesn't because it is outside the view

Vivek Nayak
  • 141
  • 1
  • 8
0

The padding is expressed in pixels for the left, top, right, and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels.

For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.

Padding can be set using the setPadding(int, int, int, int) method and queried by calling getPaddingLeft(), getPaddingTop(), getPaddingRight(), and getPaddingBottom().

Margins are the spaces outside the border, between the border and the other elements next to this view. Note that, like the padding, the margin goes completely around the content :there are margins on the top, bottom, right, and left sides. Margin can be set using the setMargins(int left, int top, int right, int bottom) method.