Difference between a View's Padding and Margin
Asked Answered
P

15

587

What is the difference between a View's Margin and Padding?

Pilocarpine answered 6/1, 2011 at 21:11 Comment(4)
Padding is inside of the border, margin is outside. See the W3C Box model for details. This blog post is much more readable, though :-)Deemster
This may be helpful Declaring LayoutChromite
this is the same as HTML, see here for more #2189952Heterophyllous
See my answer on similar question #21959550Ideational
S
599

To help me remember the meaning of padding, I think of a big coat with lots of thick cotton padding. I'm inside my coat, but me and my padded coat are together. We're a unit.

But to remember margin, I think of, "Hey, give me some margin!" It's the empty space between me and you. Don't come inside my comfort zone -- my margin.

To make it more clear, here is a picture of padding and margin in a TextView:

enter image description here

xml layout for the image above

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView margin only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:padding="10dp"
        android:textColor="#000000"
        android:text="TextView padding only"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:padding="10dp"
        android:text="TextView padding and margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#c5e1b0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f6c0c0"
        android:textColor="#000000"
        android:text="TextView no padding no margin"
        android:textSize="20sp" />

</LinearLayout>

Related

Slurp answered 27/8, 2014 at 6:30 Comment(0)
C
583

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.

An image says more than 1000 words (extracted from Margin Vs Padding - CSS Properties):

alt text

Conciliator answered 6/1, 2011 at 21:15 Comment(5)
The answer is for HTML/CSS, the question was about Android. Android's view model is inspired by HTML, but not identical. For one thing, the border is not a first-class sizable object there.Bhutan
NOTE: In Android, the layout_width property includes content and padding. (In HTML, the css width property refers to content width only.) As Seva said, Android has no built in concept of borders. You can use a 9-patch png background or an xml vector drawable to add a border in Android.Omnipotence
It's also worth noting that the background is modified based on the margin but not the padding (in Android.)Humorist
In Android, what is here referred to as the "border", is actually the 'view container'. This should clarify things a bit I would hope.Foretime
In general I'd agree with the saying about the image and 1K words, but here the legend destroys clarity. Instead, the word Margin could be inside the blue region, and the word Padding inside the yellow region. Then the saying would apply. For now, the textual analogy with the coat in the accepted answer is, for me, more clear than the image here, in much less than 1K words :-) In fact, the image is even wrong. The white around the text Content is part of Padding.Obstructionist
Q
76

Padding is inside of a View.

Margin is outside of a View.

This difference may be relevant to background or size properties.

Quinonez answered 6/1, 2011 at 21:15 Comment(0)
B
41

Padding is within the view, margin is outside. Padding is available for all views. Depending on the view, there may or may not be a visual difference between padding and margin.

For 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.

For TextViews, on the other hand, the visual effect of padding and margin is identical.

Whether or not margin is available is determined by the container of the view, not by the view itself. In LinearLayout margin is supported, in AbsoluteLayout (considered obsolete now) - no.

Bhutan answered 6/1, 2011 at 21:14 Comment(0)
H
24

Below image will let you understand the padding and margin-

enter image description here

Hairless answered 4/2, 2015 at 8:54 Comment(0)
C
9

Padding means space between widget and widget original frame. But the margin is space between widget's original frame to boundaries other widget's frame.enter image description here.

Chauncey answered 1/2, 2017 at 4:58 Comment(0)
R
8

Padding
Padding is inside of a View.For example if you give android:paddingLeft=20dp, then the items inside the view will arrange with 20dp width from left.You can also use paddingRight, paddingBottom, paddingTop which are to give padding from right, bottom and top respectively.

Margin
Margin is outside of a View. For example if you give android:marginLeft=20dp , then the view will be arranged after 20dp from left.

Rationality answered 3/7, 2015 at 4:24 Comment(0)
F
7

Padding is the space inside the border between the border and the actual image or cell contents. Margins are the spaces outside the border, between the border and the other elements next to this object.

Fez answered 2/11, 2012 at 8:19 Comment(0)
M
7

Sometimes you can achieve the same result by playing only with padding OR margin. Example :

Say View X contains view Y (aka : View Y is inside View X).

-View Y with Margin=30 OR View X with Padding=30 will achieve the same result: View Y will have an offset of 30.

Magritte answered 6/12, 2012 at 16:25 Comment(0)
G
3

Let's just suppose you have a button in a view and the size of the view is 200 by 200, and the size of the button is 50 by 50, and the button title is HT. Now the difference between margin and padding is, you can set the margin of the button in the view, for example 20 from the left, 20 from the top, and padding will adjust the text position in the button or text view etc. for example, padding value is 20 from the left, so it will adjust the position of the text.

Grogan answered 12/9, 2014 at 10:19 Comment(0)
M
3

In addition to all the correct answers above, one other difference is that padding increases the clickable area of a view, whereas margins do not. This is useful if you have a smallish clickable image but want to make the click handler forgiving.

For eg, see this image of my layout with an ImageView (the Android icon) where I set the paddingBotton to be 100dp (the image is the stock launcher mipmap ic_launcher). With the attached click handler I was able to click way outside and below the image and still register a click.

enter image description here

Mood answered 8/1, 2018 at 20:27 Comment(0)
H
3

In simple words:

  1. Padding - creates space inside the view's border.
  2. Margin - creates space outside the view's border.
Homotaxis answered 28/2, 2018 at 5:51 Comment(0)
C
2

Margin refers to the extra space outside of an element. Padding refers to the extra space within an element. The margin is the extra space around the control. The padding is extra space inside the control.

It's hard to see the difference with margin and padding with a white fill, but with a colored fill you can see it fine.

Child answered 3/7, 2015 at 2:24 Comment(0)
A
2

In simple words:
padding changes the size of the box (with something).
margin changes the space between different boxes

Acherman answered 2/8, 2015 at 11:12 Comment(0)
G
1

Padding is used to add a blank space between a view and its contents.

enter image description here

Margin is used to add a space between different views.

enter image description here For both padding and margin, we have two way to set those,

  • setting all sides with equal value
  • setting side specific values as per requirement

All sides with equal values:

You can use android:padding="15dp" for setting padding of 15dp all the sides

and android:layout_margin="15dp" for setting margin of 15dp all the sides

Sides with specific values:

Padding

  • android:paddingBottom Sets the padding at the bottom edge
  • android:paddingStart Sets the padding at the start edge means at the left side of view
  • android:paddingEnd Sets the padding at the end edge means at the right side of view
  • android:paddingTop Sets the padding at the top edge

Margin

  • android:layout_marginBottom Specifies extra space on the bottom side of this view.
  • android:layout_marginEnd Specifies extra space on the end side, means at the right side of this view.
  • android:layout_marginStart Specifies extra space on the start side, means at the left of this view.
  • android:layout_marginTop Specifies extra space on the top side of this view.
Gavage answered 30/5, 2021 at 1:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.