Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase the grid spacing in android

Tags:

android

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?

like image 215
James Avatar asked Oct 24 '11 04:10

James


People also ask

What is Grid view android?

android.widget.GridView. A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.

What can I use instead of grid layout in android?

Some of the most commonly used layouts are LinearLayout, Constraint Layout, RelativeLayout, and GridLayout. Suppose you need to display the elements linearly, whether horizontally or vertically; you can use LinearLayout.


2 Answers

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> 
like image 83
Paresh Mayani Avatar answered Oct 13 '22 23:10

Paresh Mayani


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.

like image 43
Олег Місько Avatar answered Oct 14 '22 00:10

Олег Місько