Dipali
Dipali

Reputation: 1158

How to remove the space between rows of grid view in android

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.

Upvotes: 19

Views: 31463

Answers (8)

Sana
Sana

Reputation: 456

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

Upvotes: 0

user5077219
user5077219

Reputation:

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

Upvotes: 0

Deepak Sood
Deepak Sood

Reputation: 405

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"/>

Upvotes: 3

Hardip
Hardip

Reputation: 360

<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>

Upvotes: 0

Dipali
Dipali

Reputation: 1158

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

Upvotes: 43

akkilis
akkilis

Reputation: 1934

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.

Upvotes: 0

Nick Campion
Nick Campion

Reputation: 10479

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.

Upvotes: 0

himanshu
himanshu

Reputation: 1980

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

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

I hope this will help you.

Upvotes: 0

Related Questions