EM10
EM10

Reputation: 815

Image spacing between columns in a gridview

I have a gridview in which I have images. The problem I have is that there is spacing between the columns. I don't want any spacings. If you check the picture below I don't have spacing between the rows.

enter image description here

Here is my gridview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<GridView
    android:id="@+id/gridview"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:numColumns="6"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:listSelector="@android:color/transparent">
</GridView>

Here is the code:

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);

    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

What I want to do is to have same amount of spacing between all the images.

Upvotes: 0

Views: 1494

Answers (3)

Piyush
Piyush

Reputation: 18933

Remove this line from your GridView.

 android:stretchMode="columnWidth"

Change this

 android:stretchMode="none"

UPDATE:

set this property for your ImageView.

    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));

Use:

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth" >
</GridView>

 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
 imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
 imageview.setpadding(8,8,8,8);

Upvotes: 1

Hamid Shatu
Hamid Shatu

Reputation: 9700

Add these two attributes....

    android:horizontalSpacing="2dip"
    android:verticalSpacing="2dip"

Update: Set android:stretchMode attribute value to none instead of columnWidth...this will fix the space problem between columns.

android:stretchMode="none"

Upvotes: 1

bGorle
bGorle

Reputation: 1996

try this

 android:horizontalSpacing="10dp"
 android:verticalSpacing="10dp"

Upvotes: 1

Related Questions