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?
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.
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.
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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With