GridView rows outer margin
Asked Answered
E

2

32

I cannot find out a way to add an outer margin to the GridView rows. I found the setHorizontalSpacing and setVerticalSpacing properties which add inner padding between the GridView rows, however it doesn't apply to the outer borders of the rows.

I'd like to have a result as it's featured on the Google Play store:

enter image description here

Thanks!

Escurial answered 17/6, 2013 at 0:12 Comment(4)
Did you try to simply add padding (or a margin) to the GridView?Dulcet
as @MH you can add the android:paddingLeft/Right properties with the same value of setVertical/HorizontalSpace. The drawback is that this value is fixed, and if you set android:stretchMode="spacingWidth" the resizing will modify only the inner spacings.Cryostat
Adding padding to the GridView works as expected. However when the items start to scroll the padding remains fixed - i.e. when one of the items is hanging off the top of the screen there is a a white (in my case) strip between the top of the screen and the item. It would look way better if the margin was on the item inside the GridView, but those margins don't appear to be respected.Debouchment
The only way I found to do this, is either use a ListView and add logic to create rows that looks like grid items, or extending ViewGroup and create a layout that supports this. I've found this library that uses the second approach: github.com/thquinn/DraggableGridViewEscurial
H
95

You can use android:clipToPadding=false to add padding that doesn't stay when the items are scrolled. Like this

<GridView
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:horizontalSpacing="16dp"
    android:verticalSpacing="16dp"
    android:padding="16dp"
    android:clipToPadding="false" />

And if you want the scrollbar on the outside of the padding area, set android:scrollbarStyle="outsideOverlay" thanks @Karl!

Howling answered 31/10, 2013 at 19:42 Comment(4)
Also use: android:scrollbarStyle="outsideOverlay". This puts the scrollbar in the padding area. Otherwise it will be before the padding.Nellenelli
This was incredibly hard to find and not very intuitive. Thanks :)Hypertonic
Thanks. This is also useful for preventing the currently selected row to not be off screen due to TV overscanning.Pyemia
what if i don't want to use padding? because my HeaderGridView has a header and i want it to fit the screen width!Sweepings
M
0

If you want result like GooglePlay store, you can set setHorizontalSpacing and setVerticalSpacing and then set layout_margin to GridView.

Manicotti answered 1/12, 2019 at 11:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.