Sinmok
Sinmok

Reputation: 656

Removing spacing between ListView rows using a custom Adapter

I'm trying to figure out how to remove spacing between rows in a ListView using a custom adaptor that returns an ImageView in the getView() function.

Visual representation of the issue: enter image description here

The ListView is currently sitting inside a RelativeLayout.

I have tried setting the padding, divider and margins all to 0dp on the ListView with no luck. I have also tried setting the padding on each individual ImageView item within the getView() function - all with no luck.

How do I remove all padding/margins/spacing between the ListView rows? I am open to any solution whether Java or XML based.

ListView XML:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView1"
    android:layout_below="@+id/img1"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/textView"
    android:fastScrollEnabled="false"
    android:divider="@null"
    android:padding="0dp"
    android:layout_margin="0dp"
    android:dividerHeight="0dp"
    />

Relevant getView() adaptor code

#some irrelevant code to load images

viewGroup.setClipToPadding(false);
viewGroup.setPadding(0,0,0,0);


if(view == null){
    ImageView newView = new ImageView(this.context);
    newView.setImageDrawable(img);
    newView.setPadding(0,0,0,0);
    return newView;
}
else{

    ((ImageView) view).setImageDrawable(img);
    return view;
} 

Upvotes: 1

Views: 779

Answers (1)

Sinmok
Sinmok

Reputation: 656

Figured it out. I needed to use setAdjustViewBounds on the new ImageView.

Example from original code in getView():

        if(view == null){
            ImageView newView = new ImageView(this.context);
            newView.setImageDrawable(img);
            newView.setPadding(0,0,0,0);
            newView.setAdjustViewBounds(true);
            return newView;
        }
        else{

            ((ImageView) view).setImageDrawable(img);
            return view;
        }

Upvotes: 1

Related Questions