Loic Reyreaud
Loic Reyreaud

Reputation: 52

Remove Blank Space ListView Android

I'm working on a custom listview and each row use 2 image view as a background. Everything working great except the fact that a blank space between each item remain. Here's my code:

First my list_row.xml. Note that the 2 images views are the picture and a dark overlay which make the first image darker and the text much easier to read. They act as the background for each row.

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

<!-- BG Image -->
<ImageView
    android:id="@+id/image_bg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"/>

<!-- Dark Overlay for BG Image -->
<ImageView
    android:id="@+id/image_bg_over"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true" />

<TextView
    android:id="@+id/filler"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:layout_centerHorizontal="true" />

<!-- Bar Name -->
<TextView
    android:id="@+id/barname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/filler"
    android:textSize="@dimen/title"
    android:textStyle="bold"
    android:textColor="@color/list_text_color"/>

<!-- Address -->
<TextView
    android:id="@+id/address"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/barname"
    android:gravity="center"
    android:layout_marginTop="1dip"
    android:textColor="@color/list_text_color"
    android:textSize="@dimen/rating"/>

<!-- Text CheckIn / CheckOut -->
<TextView
    android:id="@+id/check"
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/address"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:textColor="@color/list_text_color"
    android:textSize="@dimen/genre"/>

<!-- Image CheckIn / CheckOut -->
<ImageView
    android:id="@+id/checkImg"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/address"
    android:gravity="right"
    android:layout_toRightOf="@+id/check"
    android:adjustViewBounds="true" />


</RelativeLayout>

Then my adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    ImageView bgImg = (ImageView) convertView.findViewById(R.id.image_bg);
    ImageView bgImgOver = (ImageView) convertView.findViewById(R.id.image_bg_over);

    TextView barname = (TextView) convertView.findViewById(R.id.barname);
    TextView address = (TextView) convertView.findViewById(R.id.address);
    ImageView checkImg = (ImageView) convertView.findViewById(R.id.checkImg);
    TextView check = (TextView) convertView.findViewById(R.id.check);

    Bar m = barItems.get(position);

    bgImg.setImageResource(R.drawable.bg_default);
    bgImgOver.setImageResource(R.drawable.bg_darkover);

    barname.setText(m.getPlaceName());
    address.setText(m.getAddress());
    check.setText("Check-In");
    checkImg.setImageResource(R.drawable.ic_checkin);

    return convertView;
}

The xml of the fragment:

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

<ListView
    android:id="@+id/places_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</RelativeLayout>

The onViewCreated method of the fragment in which I create the listview:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) { //Otherwise can't use findViewById
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new CustomListAdapter(getActivity(), barList);
    listView.setAdapter(adapter);

    Bar bar1 = new Bar();
    bar1.setPlaceName("place1");
    bar1.setAddress("Addr1");
    barList.add(bar1);
    Bar bar2 = new Bar();
    bar2.setPlaceName("place2");
    bar2.setAddress("Addr2");
    barList.add(bar2);
    Bar bar3 = new Bar();
    bar3.setPlaceName("place3");
    bar3.setAddress("Addr3");
    barList.add(bar3);
    adapter.notifyDataSetChanged();

    listView.setDivider(null);
    listView.setDividerHeight(0);
}

Using setDivider I can delete a black divider inside the blank space, but not the blank space. You can see the space in the screenshot listView_blank_space

I think I did a mistake inside the relative layout which create this space but I can't find where, any help appreciated!

Upvotes: 0

Views: 1066

Answers (2)

Stepan Maksymov
Stepan Maksymov

Reputation: 2606

<!-- BG Image -->
<ImageView
    android:id="@+id/image_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"/>

same options for <!-- Dark Overlay for BG Image -->

and for the top item RelativeLayout set fixed height;

Upvotes: 1

I think your image background not enough height,add this 2 attribute to image background and see

 android:scaleType="centerCrop"

Upvotes: 0

Related Questions