27

I have a grid layout which is filled with buttons. I want the buttons to be more distant from each other, what code should I write? I tried to search for it but only found a solution for GridView, not GridLayout.

Jasper
  • 1,195
  • 8
  • 22
Ralph
  • 325
  • 1
  • 4
  • 13
  • 2
    check this answer if it helps http://stackoverflow.com/a/21456391/1320616 – Ankit Aggarwal Feb 08 '16 at 13:09
  • 2
    Possible duplicate of [GridLayout(not GridView) - Spaces between the cells](http://stackoverflow.com/questions/21455495/gridlayoutnot-gridview-spaces-between-the-cells) – Rohit5k2 Feb 08 '16 at 13:10
  • These don't seem to be duplicates. This is about adding spacing; the [other](https://stackoverflow.com/questions/35270404/android-gridlayout-spacing-between-items) is about removing it. – Ryan M Oct 06 '20 at 09:44

2 Answers2

26

You can put margin to the buttons

Stefano
  • 3,017
  • 2
  • 26
  • 33
  • 10
    what if I have to dynamically rearrange columns and rows? the margin will mess everything up in that case. – Alberto M Feb 13 '18 at 14:31
  • 4
    margin will not show equal spacing on each side. For example: margin of 2dp on each item will show 2 dp on each side but item after will also have 2 dp on each side. Making it total of 4dp space in between now. – Alex Jan 08 '19 at 19:14
  • You can put only margin end on the individual items to avoid duplication of margins. – David Kariuki Feb 12 '19 at 20:04
24

Instead of adding margin to each buttons, use android:useDefaultMargins="true"

It is better than adding margin to each buttons!

Jarin Rocks
  • 855
  • 8
  • 15
  • this doesn't answer the main question. Does this answer my doubts on the first answer? In that case you should've commented the answer. – Alberto M Feb 13 '19 at 11:35
  • 1
    Even if you re-arrange items they will stay distinct and wont mess up with each other – Jarin Rocks Feb 13 '19 at 13:47