Android Padding vs Margin

      No Comments on Android Padding vs Margin

Android padding vs margin is a very frequently asked question. Most of the beginners miss understand it. So here is a little help from my side. The idea behind android margin and padding is very similar to CSS margin and padding. Lets understand it with examples.

Margin

Margin is a way for a view to enforce some distance from others views. By specifying margin for a view, we say that keep this much distance from this view. Android has 5 kinds of margins.

  1. margin – keep distance on all the four sides
  2. marginLeft – keep distance from left side of the view
  3. marginRight – keep distance from right side of the view
  4. marginTop – keep distance from top of the view
  5. marginBottom – keep distance from bottom of the view

Example

margin

In the above example there is a LinearLayout(Parent of all views) which does not have  any margin specified. But the textView has a margin of 20dp, and according to our earlier definition the text view is saying that keep 20dp distance from me in all the sides. That is why the textView has the 20dp space in all the directions. Then there is LinearLayout sibling of textView which says in left, right and bottom keep 10dp distance from me and hence there is space in all 3 sides.

Padding

Padding is a way to push the contents away from view’s inner boundary. When we specify the padding of a view, we say the content to keep this much distance from your inner boundary(left, right, top or bottom). Like margin, padding is also of 5 types.

  1. padding – keep distance from all the inner boundaries
  2. paddingLeft – keep distance from the left inner boundary
  3. paddingRight – keep distance from the right inner boundary
  4. paddingTop – keep distance from the top inner boundary
  5. paddingBottom – keep distance from the bottom inner boundary

Example

padding

Above you can see example of all kinds of paddings applied to a textView. So at the end the difference between margin and padding is that margin keeps the other views away from a view and padding pushes its contents away from view’s own inner boundaries.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *