19

In android grid view there is extra space between the rows of grid view. I am not adding any space to the gridview.

here is my xml files

        <GridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:horizontalSpacing="5px"
                android:numColumns="3"
                android:columnWidth="50dip"
                 android:verticalSpacing="0dip"
                android:gravity="center"
                />

and the other xml for the imageview which is adding view to the gridview is

        <ImageView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/singlePhoto"
            android:layout_gravity="top"
            android:layout_width="145dip"
            android:layout_height="145dip"
            android:layout_marginTop="0dip"
            android:layout_marginBottom="0dip"
            >
        </ImageView>

In Adapter class the code is

        if(mView == null){
            mView = mLayoutInflater.inflate(R.layout.newsgridthumbpic, null);
                }

                ImageView mImage = (ImageView)mView.findViewById(R.id.singlePhoto);
                PhotoGridInfoSet mInfo = (PhotoGridInfoSet)details.get(position);
                if(mImage != null){
                mImage.setTag(mInfo.getPhotoThumbNailString());
                mImage.setVerticalScrollBarEnabled(true);
                mImage.setVerticalFadingEdgeEnabled(true);
                }
        }!

The space is between the rows of gridview. I have not added any space anywhere in the code Still the there is vertical spacing.I am not able understand why there is space in between gridview rows. This space is not appearing in the hdpi device/emulator. This problem is in the mdpi and ldpi devices/emulator.

Image for reference. this is what happening right now.

Dipali
  • 1,148
  • 3
  • 13
  • 23

8 Answers8

43

I got the solution for this problem.

To solve this i need to add 1 line in my xml file

android:adjustViewBounds="true"

and after adding this. the space is not appearing now

duggu
  • 37,851
  • 12
  • 116
  • 113
Dipali
  • 1,148
  • 3
  • 13
  • 23
2

Just put last 2 lines in gridView

<GridView
        android:numColumns="2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/gridView"
        android:horizontalSpacing="0dip"
        android:verticalSpacing="0dip"/>
Deepak Sood
  • 385
  • 5
  • 16
0

Setting android:verticalSpacing in the gridview to negative values. will remove the vertical space.

(Eg: android:verticalSpacing="-10dp")

test with some number to get the right value

Community
  • 1
  • 1
0
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:horizontalSpacing="5px"
    android:numColumns="3"
    android:columnWidth="50dip"
     android:verticalSpacing="0dip"
    android:gravity="center"/>

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/singlePhoto"
    android:layout_width="145dip"
    android:layout_height="145dip"
    android:scaleType="fitXY">
</ImageView>
Hardip
  • 360
  • 3
  • 9
0

I have another way as a fixing for the same issue while one using RecyclerView.

Just pass the number of SpanCount like I have given 6 in GridLayoutManager(this, 6) and change the SpanCount to reduce or increase the gap between the items in the RecyclerView.

Check this sample code-

GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 6);
  rvSizes.setLayoutManager(gridLayoutManager);
  SizeSelectorAdaptersizeChooserAdapter = new SizeSelectorAdapter(this, sizes);
  rvSizes.setAdapter(sizeChooserAdapter);

Hope this will be useful. Thank you

Sana
  • 456
  • 3
  • 9
0

Put the following line in ur getView() method...

mImage.setPadding(0, 0, 0, 0);

I hope this will help you.

himanshu
  • 1,990
  • 3
  • 18
  • 36
0

It looks like your picture is being scaled to fit the column width of 50dp while maintaining the aspect ratio. This is introducing a bunch of unfilled space in the vertical layout. If you want your column to be 50dp wide, the easiest way to work out these spacing issues will be to adjust your imageview to have a width of 50dp and adjust the imageviews height proportional to that.

Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • Even if i change it to dip it is showing the space between rows and the prob is about vertical spacing – Dipali Feb 03 '12 at 08:24
  • The height of the views you are placing in the grid define the grids height. that mixed with your gravity being set to "top" is what is causing your grid to look like that. – Nick Campion Feb 03 '12 at 16:01
0

just check that your "newsgridthumbpic" layout does not contain the extra padding or any background image having a large height that your grid view element.

Also double check that you are supplying column_width as 50dip but your "imageview's" width is 145 dip.

akkilis
  • 1,904
  • 14
  • 21
  • yea i have tried all that things changing the imageview height n width and the images are coming from server thats why there is no background or src images – Dipali Feb 03 '12 at 11:29