6

I want to remove any space between different items in a ListView. Code:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/wrapper"
    android:padding="0dp"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:textColor="#000000"/>

</LinearLayout>

And Listview

<ListView
        android:id="@+id/listView1"
        android:transcriptMode="alwaysScroll"
        android:stackFromBottom="true"
        android:dividerHeight="0dp"
        android:divider="@null"
        android:listSelector="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

But there is still some space between the items. Can anyone help me?

Screenshot

Phil
  • 583
  • 4
  • 7
  • 19
  • Don't use "fill_parent" use "match_parent", also, padding=0dp is not needed in the LinearLayout (0 is the default). The ListView should match_parent height but without seeing the full XML is hard to tell what your listview is doing. Post a screenshot of the effect. – Martin Marconcini Jun 23 '14 at 18:40
  • Additionally, try adding a different (red, green, etc.) background color to your widgets to see which one is leaving gaps. – Martin Marconcini Jun 23 '14 at 18:42
  • See the edit, the red marked space is the space I want to remove – Phil Jun 23 '14 at 19:02
  • Take a look at [this](http://stackoverflow.com/questions/6593885/how-to-remove-the-top-and-bottom-space-on-textview-of-android) question. – trylimits Jun 23 '14 at 19:08

2 Answers2

12

The line android:dividerHeight="10dp" causes the gaps between your lines. I color-coded the overall UI:

Color coded UI

Once I set the dividerHeight line above from "10dp" to "0dp" I got this:

Color coded- no dividerHeight

Ok, so here is the full set of code I used so you can see where you may have gone wrong.

MainActivity:

package com.ds.listviewtest;

import android.app.ListActivity;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    static final String[] FRUITS = new String[] {
            "Apple", "Avocado", "Banana",
            "Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
            "Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setListAdapter(new SimpleAdapter(FRUITS));
        setContentView(R.layout.activity_main);
    }

    private class SimpleAdapter implements ListAdapter
    {
        String[] items;

        public SimpleAdapter(String[] items)
        {
            this.items = items;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return items.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return items[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.comment);
            textView.setText(items[position]);

            return rowView;
        }

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 1;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean areAllItemsEnabled() {
            // TODO Auto-generated method stub
            return true;
        }

        @Override
        public boolean isEnabled(int position) {
            // TODO Auto-generated method stub
            return true;
        }

    }

}

Here is list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@android:color/holo_orange_dark"
    android:id="@+id/wrapper"
    android:padding="0dp"
    android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/comment"
            android:background="@android:color/holo_blue_light"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:text="This is a test comment"
            android:textColor="#000000"/>

</LinearLayout>

Finally here is activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/holo_green_light"
        android:dividerHeight="0dp" />  <!-- EDIT THIS VALUE HERE TO 0DP -->

</RelativeLayout>
David S.
  • 6,567
  • 1
  • 25
  • 45
  • 2
    Thats exactly what I was looking for, but look: I already set the height to 0dp o.O – Phil Jun 23 '14 at 19:24
  • I added the code I used. You should be able to create a new Android app, replace MainActivity with the one above, and add in the two layout XML files and see exactly what I've shown above. Hopefully this will help. – David S. Jun 23 '14 at 19:36
  • 1
    This was not my problem, but your catch to colour the background of all element with different colours helped me a lot with some similar issue. I was getting crazy as where i was making mistake, and colouring it gave me the exact position where the mistake was and now working like a charm.. Kudos.. – Malav Shah Nov 25 '16 at 17:28
3

Simply, give your divider height a negative value.
Example :
android:dividerHeight="-20dp"
This will remove spaces between ListView values.

Hussein Nd
  • 33
  • 3