45

I have a gridview in which i have a lot of items in three colums. I want to increase the spacing between them. How can I do that in android?

James
  • 13,891
  • 26
  • 68
  • 93

3 Answers3

140

You can use android:verticalSpacing and android:horizontalSpacing in GridView tag and provide the spacing as per your requirement.

For example:

  <GridView
        android:layout_height="wrap_content"
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"       // space between two items (horizontal)
        android:verticalSpacing="10dp">        // space between two rows (vertical)
  </GridView>
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • the answer you stated here provides spacing between the grid items but how do i get spacing between the 1st row grid items and the action bar? – Ram Patra Jul 03 '14 at 18:03
  • 2
    @Ramswaroop can't you use margin for that? – Paresh Mayani Jul 03 '14 at 18:05
  • I tried with `marginTop` on the grid item but it isnt working, i also applied `paddingTop` in `GridView` and i got spacing but when i scroll the spacing still remains, see screenshot for more clarity http://imgur.com/LBrB6N5 – Ram Patra Jul 03 '14 at 18:16
  • 3
    @Ramswaroop use padding, and on the GridView `android:clipToPadding="false"` – ataulm Apr 19 '15 at 18:57
3

I was trying to use

android:horizontalSpacing android:verticalSpacing

and it gave me no success.

So basically I've added margins to all my elements. Maybe, it's a better point out there, because you can configure like EVERY element by this way.

This is what it looks like in my way

<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="2"
android:rowCount="3">
<ImageView
  android:id="@+id/imageView2"
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_recipe"
  android:layout_marginBottom="30dp"
  android:layout_marginRight="15dp"/>

<ImageView
  android:id="@+id/imageView3"
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_add_recipe"
  android:layout_marginBottom="30dp"
  android:layout_marginLeft="15dp"/>

<ImageView
  android:id="@+id/imageView4"
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_favorite_recipies"
  android:layout_marginBottom="30dp"
  android:layout_marginRight="15dp"/>

<ImageView
  android:id="@+id/imageView6"
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_information"
  android:layout_marginBottom="30dp"
  android:layout_marginLeft="15dp"/>

<ImageView
  android:id="@+id/imageView5"
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_settings"
  android:layout_marginRight="15dp"/>

<ImageView
  android:layout_width="128dp"
  android:layout_height="128dp"
  app:srcCompat="@drawable/ic_exit"
  android:layout_marginLeft="15dp"/>

So I have six images in GridLayout. Two in a row and three rows.

2

use this in the BaseAdapter

imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
imageView.setPadding(1, 1, 1, 1);
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166