How to get rid of spaces in gridview
Asked Answered
J

4

12

This thing has been doing my head in for a little while now and I can't seem to be able to solve it.

I've got a grid view, in which I display a few bitmaps. I add a padding of 5dp on all image sides when filling up my image view via the adapter (iv.setPadding(5, 5, 5, 5);), but somehow, there's more space being added to the top and bottom on each item on my gridview.

I know this has already been asked a few times here, and some of them even have accepted answers, however, when I try to sue the answers as described on the URL's above, I still keep getting the same extra spaces on top and bottom.

Is there any other way of doing this nowadays?

Here's some of my code:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageGrid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numColumns="6"
    />

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<ImageView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_item_image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />

I have tried adding the following to my listselector attribute in main.xml

  • @null
  • @drawable/solid_white
  • @android:id/empty

Here's how it looks like with the extra spaces on top and bottom (the ones on the sides are the ones I'm adding as described above):

enter image description here

Any other ideas as to how to get those bloody spaces removed so my grid view has the same spacing?

Thanks in advance,

Jacksonjacksonville answered 13/11, 2011 at 0:11 Comment(0)
B
9

If not otherwise set, a GridView will override the layout you're imagining it should produce by stretching things in various ways. If you want to have full control of the layout by defining the column widths and the size of the grid items, then you have to make sure to set the GridView's stretchMode property to "none".

<GridView
        ...
        android:stretchMode="none">
</GridView>

Also, you are filling the parent with your GridView width. I think you should be wrapping the content for the width unless you want extra filling added. Where are you adding the 5dp padding? Around each bitmap or around the edge of the gridview? I don't see it.

You should probably be filling the height though. (in the following some of the width and heights can probably be replaced by "wrap_content")

So, for you

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageGrid"
    android:stretchMode="none"        
    android:layout_width="XXXdp"
    android:layout_height="fill_content"
    android:numColumns="6"
    />

Also, not sure if you need column spacing and width, which would be something like (or a dp instead of wrap_content) - you may not need these in your GridView - I'm not sure:

    android:columnWidth="YYYdp"
    android:horizontalSpacing="0dp"

ImageViews with 5dp padding:

<?xml version="1.0" encoding="utf-8"?>
<ImageView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_item_image"
   android:padding="5dp"
   android:layout_width="UUUdp"
   android:layout_height="VVVdp" />
Bronchia answered 13/11, 2011 at 0:13 Comment(5)
That stopped my gridview from displaying anything at allJacksonjacksonville
I add it to my images when filling up the gridview via the adapter. I've tried wrapping the content, but still, when using android:stretchMode="none", everything stops appearingJacksonjacksonville
@Marcos - I had similar problems. It took a bit of tweaking, but the stretchMode should definitely be "none," or Android changes the size of your grid item. -------------- Try setting the columnWidths explicitly to "Xdp". Also, try calculating how wide the entire grid view should be and setting the layout_width explicityly to "Ydp".Bronchia
somehow android:stretchMode still makes it disappear. Take it off and it's back, but still with the unwanted spaces :-(Jacksonjacksonville
@Marcos - Does it disappear in the Graphical Layout too - I found changing properties and playing around using the Graphical Layout very helpful. Play around with the various widths and heights. And start explicitly defining the gridview widths and filling the gridview height and explicitly defining the imageview widths and heights. When you can get things to show up like that, then you can tweak it from there.Bronchia
S
24

For me it solved the problem by setting imageview.setAdjustViewBounds(true); in my Adapter class.

It seems that my square images were automatically resized in width but Gridview still used the original hight. By adjusting bounds, both dimensions were resized automatically and all items were displayed smoothly even with default stretchmode enabled.

Stationery answered 21/11, 2011 at 11:40 Comment(2)
This works for square views (images), with: View.setPadding(0, 0, 0, 0); and xml: android:verticalSpacing="0dp" android:horizontalSpacing="0dp" android:stretchMode="none"Redoubt
This should be correct answer. Other recommendations doesn't work for horizontal spacing.Helles
B
9

If not otherwise set, a GridView will override the layout you're imagining it should produce by stretching things in various ways. If you want to have full control of the layout by defining the column widths and the size of the grid items, then you have to make sure to set the GridView's stretchMode property to "none".

<GridView
        ...
        android:stretchMode="none">
</GridView>

Also, you are filling the parent with your GridView width. I think you should be wrapping the content for the width unless you want extra filling added. Where are you adding the 5dp padding? Around each bitmap or around the edge of the gridview? I don't see it.

You should probably be filling the height though. (in the following some of the width and heights can probably be replaced by "wrap_content")

So, for you

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageGrid"
    android:stretchMode="none"        
    android:layout_width="XXXdp"
    android:layout_height="fill_content"
    android:numColumns="6"
    />

Also, not sure if you need column spacing and width, which would be something like (or a dp instead of wrap_content) - you may not need these in your GridView - I'm not sure:

    android:columnWidth="YYYdp"
    android:horizontalSpacing="0dp"

ImageViews with 5dp padding:

<?xml version="1.0" encoding="utf-8"?>
<ImageView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/grid_item_image"
   android:padding="5dp"
   android:layout_width="UUUdp"
   android:layout_height="VVVdp" />
Bronchia answered 13/11, 2011 at 0:13 Comment(5)
That stopped my gridview from displaying anything at allJacksonjacksonville
I add it to my images when filling up the gridview via the adapter. I've tried wrapping the content, but still, when using android:stretchMode="none", everything stops appearingJacksonjacksonville
@Marcos - I had similar problems. It took a bit of tweaking, but the stretchMode should definitely be "none," or Android changes the size of your grid item. -------------- Try setting the columnWidths explicitly to "Xdp". Also, try calculating how wide the entire grid view should be and setting the layout_width explicityly to "Ydp".Bronchia
somehow android:stretchMode still makes it disappear. Take it off and it's back, but still with the unwanted spaces :-(Jacksonjacksonville
@Marcos - Does it disappear in the Graphical Layout too - I found changing properties and playing around using the Graphical Layout very helpful. Play around with the various widths and heights. And start explicitly defining the gridview widths and filling the gridview height and explicitly defining the imageview widths and heights. When you can get things to show up like that, then you can tweak it from there.Bronchia
E
1

I found an easy way to walk around this. Just add android:layout_marginRight in your GridView.

Eurasian answered 13/9, 2012 at 7:30 Comment(0)
G
-1

step 1:

android:stretchMode="none"
android:numColumns="auto_fit"
android:gravity="center"

step 2: in onMeasure return your calculated width and height

Granulose answered 16/7, 2013 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.